当前位置:   article > 正文

Python之random和string库学习_python random string

python random string

一、random库

random是python中用来生存随机数的库。具体用法如下:

1、生成一个0到1随机浮点数

random.random()

2、生成一个a到b的随机浮点数

random.uniform(1,2)

3、生成一个a到b之间的整数

random.randint(a,b)

4、随机从序列元素中取出一个值,这个序列可以是字符串、列表或元组。

random.choice()

5、随机从序列中取出k个值,并以列表形式返回,这个序列可以是字符串、列表或元组;

random.sample(序列, k)

6、洗牌,随机打乱列表中的元素;

random.shuffle()

7、设置随机数种子,当设定好种子之后(其中x可以是任意数字),每次调用生成的随机数将会是同一个;

random.seed(x)

二、string库

1、返回大小写字母组合而成的字符串;

print(string.ascii_letters)

2、返回小写字母组合而成的字符串;

print(string.ascii_lowercase)

3、返回大写字母组合而成的字符串;

print(string.ascii_uppercase)

4、返回0-9数字组合而成的字符串;

print(string.digits)

5、返回所有特殊字符组合而成的字符串;

print(string.punctuation)

三、双库组合实例

1、random库批量生成手机号码

  1. import random
  2. def create_phone(num):
  3. # 用于存放生成的数据
  4. t_set = set()
  5. # 用于存放号段
  6. yidonglist = [134, 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 178, 182, 183, 184, 187]
  7. liantonglist = [130, 131, 132, 155, 156, 166, 185, 186, 145, 176]
  8. dianxinlist = [133, 153, 177, 173, 180, 181, 189, 199]
  9. t_list = input("请输入要生成的运营商名称(移动、联通、电信):")
  10. if t_list == "移动":
  11. t_list = yidonglist
  12. elif t_list == "联通":
  13. t_list = liantonglist
  14. elif t_list == "电信":
  15. t_list = dianxinlist
  16. else:
  17. print("请输入有效的运营商名称!!!")
  18. exit()
  19. while True:
  20. top_three = random.choice(t_list)
  21. last_eight = random.sample([str(i) for i in range(10)], 8)
  22. concat_last_eight = "".join(last_eight)
  23. t_set.add(f"{top_three}{concat_last_eight}")
  24. if len(t_set) >= num:
  25. break
  26. return t_set
  27. def start():
  28. num = int(input("请输入你想要生成多少个电话号码:"))
  29. t_set = create_phone(num)
  30. save_to_file = input("是否要将生成的号码保存到文件中?(输入 '是' 或 '否'):")
  31. if save_to_file.lower() == '是':
  32. filenames = input("请输入文件名称:")
  33. with open(filenames, 'w') as file:
  34. for nums in t_set:
  35. file.write(nums + '\n')
  36. print(f"号码已保存到文件 {filenames} 中。")
  37. elif save_to_file.lower() == '否':
  38. print("生成的号码如下:")
  39. for nums in t_set:
  40. print(nums)
  41. else:
  42. print("无效的输入。请输入 '是' 或 '否'。")
  43. start()

2、random+string库生成随机不重复车牌号

  1. import random
  2. def generate_license_plate(province, num_plates):
  3. plates = []
  4. for _ in range(num_plates):
  5. if province == "沪": # 上海车牌
  6. plate_number = f"{province}{random.choice(['A', 'B', 'C', 'D', 'E'])}{random.randint(0, 9)}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'])}{random.randint(1000, 9999)}"
  7. elif province == "京": # 北京车牌
  8. plate_number = f"{province}{random.choice(['A', 'B', 'C', 'E', 'F'])}{random.randint(0, 9)}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'])}{random.randint(10000, 99999)}"
  9. elif province == "粤": # 广东车牌
  10. plate_number = f"{province}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'])}{random.randint(1000, 9999)}"
  11. # 可以继续添加其他省份的车牌生成规则
  12. plates.append(plate_number)
  13. return plates
  14. def main():
  15. province = input("请输入省份简称(例如,沪、京、粤等): ")
  16. num_plates = int(input("请输入要生成的车牌数量: "))
  17. if province:
  18. write_to_file = input("是否要将生成的车牌号码写入文件?(输入 '是' 或 '否'):")
  19. license_plates = generate_license_plate(province, num_plates)
  20. if write_to_file.lower() == '是':
  21. filenames = input("请输入文件名称:")
  22. with open(filenames, 'w',encoding='utf-8') as file:
  23. for plate in license_plates:
  24. file.write(plate + '\n')
  25. print(f"生成的{province}省车牌号码已保存到文件 {filenames} 中。")
  26. elif write_to_file.lower() == '否':
  27. print(f"{province}省生成的车牌号码如下:")
  28. for plate in license_plates:
  29. print(plate)
  30. else:
  31. print("无效的输入。请输入 '是' 或 '否'。")
  32. else:
  33. print("请输入有效的省份简称。")
  34. if __name__ == "__main__":
  35. main()
  1. import string
  2. import random
  3. def create_license(num):
  4. t_set = set()
  5. while True:
  6. t_list = [f"鄂{i}" for i in string.ascii_uppercase.split("T")[0] if i not in ["I","O"]]
  7. top_two = random.choice(t_list)
  8. the_third = random.choice(string.ascii_uppercase)
  9. last_four = random.sample([str(i) for i in range(10)],4)
  10. concat_last_four = "".join(last_four)
  11. t_set.add(f"{top_two}·{the_third}{concat_last_four}")
  12. if len(t_set) >= num:
  13. break
  14. return t_set
  15. def start():
  16. num = int(input("请输入你想要生成多少个车牌:"))
  17. t_set = create_license(num)
  18. save_to_file = input("是否要将生成的号码保存到文件中?(输入 '是' 或 '否'):")
  19. if save_to_file.lower() == '是':
  20. filenames = input("请输入文件名称:")
  21. with open(filenames, 'w',encoding='utf-8') as file:
  22. for nums in t_set:
  23. file.write(nums + '\n')
  24. print(f"生成的湖北省车牌号码已保存到文件 {filenames} 中。")
  25. elif save_to_file.lower() == '否':
  26. print("生成的湖北省车牌号码如下:")
  27. for nums in t_set:
  28. print(nums)
  29. else:
  30. print("无效的输入。请输入 '是' 或 '否'。")
  31. start()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54577
推荐阅读
相关标签
  

闽ICP备14008679号