Python随机生成电话号码&号码段分析

引用原地址:https://blog.csdn.net/caoxinjian423/article/details/110451154

'''
搜集到以下手机号码,当然这也不全,不过也可以分析出一些规律了
中国电信号段:133,153, 180,181,189,170,173, 177,149
中国联通号段:130,131,132,155,156,185,186,145,175,176,185,171
中国移动号段:134,135,136,137,138,139,150,151,152,158,159,182,183,184,172,147,178
# 规律总结
第一位永远是 1
第二位可以是 3,4,5,7,8
第三位是由第二位决定的,有以下情况:
13 + 【0-9】
14 + 【5,7,9】
15 + 【0-9】 !4
17 + 【0-9】!4and9
18 + 【0-9】
后八位:是0-9随机

'''
import random

def getAPhoneNumber():
second = random.choice([3,4,5,7,8])
third = {
3:random.randint(0,9),
4:random.choice([5,7,9]),
5:random.choice([i for i in range(10) if i != 4]),
7:random.choice([i for i in range(9) if i != 4 ]),
8:random.randint(0,9),
}[second]

last = "".join(str(random.randint(0,9)) for i in range(8))
return "1{}{}{}".format(second,third,last)

print(getAPhoneNumber())

  

1、random.choice() 方法返回一个列表,元组或字符串的随机项

以下展示了使用 choice() 方法的实例:

#!/usr/bin/python
import random

print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
print "choice('A String') : ", random.choice('A String')  

输出结果:

choice([1, 2, 3, 5, 9]) :  2
choice('A String') :  n

2、random.randint(1,10)      # 产生 1 到 10 的一个整数型随机数  

 



 

posted @ 2021-03-01 13:58  我可以2030  阅读(1903)  评论(0编辑  收藏  举报