Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 制作随机电话号码xxx xxx xxxx_Python_Python 2.7 - Fatal编程技术网

Python 制作随机电话号码xxx xxx xxxx

Python 制作随机电话号码xxx xxx xxxx,python,python-2.7,Python,Python 2.7,它将返回一个随机电话号码xxx xxx xxxx,但有以下限制: 区号不能以零开头 中间三位数字不能是9 中间三位数字不能是000 最后4位数字不能全部相同 让我们从区号开始。没有前导零,所以只在1和9之间选择。那么剩下的两个可以是00到99之间的任何值 def makeFirst(): first_digit = random.randint(1,9) remaining = random.randint(0,99) return first_digit*100 +

它将返回一个随机电话号码xxx xxx xxxx,但有以下限制:

  • 区号不能以零开头
  • 中间三位数字不能是9
  • 中间三位数字不能是000
  • 最后4位数字不能全部相同
让我们从区号开始。没有前导零,所以只在1和9之间选择。那么剩下的两个可以是00到99之间的任何值

def makeFirst():
    first_digit = random.randint(1,9)
    remaining = random.randint(0,99)
    return first_digit*100 + remaining
接下来是中间的数字。它们不能有9个so样本0到8。然后循环直到你们得到一个有效的案例,若你们碰巧抽样了1000个,那个么就扔掉它

def makeSecond():
    middle = 0
    while middle == 0:
        middle1 = random.randint(0,8)
        middle2 = random.randint(0,8)
        middle3 = random.randint(0,8)
        middle = 100*middle1 + 10*middle2 + middle3
    return middle
对于最后四个数字,我们将使用
random.sample
,以确保不会出现任何重复

def makeLast():
    return ''.join(map(str, random.sample(range(10),4)))
最后,将整个过程连接在一起,并将其格式化为电话号码

def makePhone():
    first = makeFirst()
    second = makeSecond()
    last = makeLast()
    return '{3}-{3}-{4}'.format(first,second,last)
一些测试

for i in range(5):
    print makePhone()

425-426-8902
473-775-2793
434-624-8356
287-630-4560
861-431-7659

稍微简单一点的解决方案

import random

def phn():
    n = '0000000000'
    while '9' in n[3:6] or n[3:6]=='000' or n[6]==n[7]==n[8]==n[9]:
        n = str(random.randint(10**9, 10**10-1))
    return n[:3] + '-' + n[3:6] + '-' + n[6:]
以及每次第一次返回的解决方案(无while循环)


我试图结合OP、@kgull、@Cyber的代码和@ivan_pozdeev的关注点,也满足OP的要求:

>>> def gen_phone():
    first = str(random.randint(100,999))
    second = str(random.randint(1,888)).zfill(3)

    last = (str(random.randint(1,9998)).zfill(4))
    while last in ['1111','2222','3333','4444','5555','6666','7777','8888']:
        last = (str(random.randint(1,9998)).zfill(4))

    return '{}-{}-{}'.format(first,second, last)

>>> for _ in xrange(10):
    gen_phone()

'496-251-8419'
'102-665-1932'
'262-624-5025'
'230-459-3242'
'355-131-0243'
'488-001-6828'
'244-539-2369'
'896-547-4539'
'522-406-8256'
'789-373-4240'

最后一位-
c
是否应该有4位数字?您需要学习如何在循环时使用
。具体来说,您需要在每个随机数生成器周围创建一个循环,该循环仅在随机数与您的条件匹配时才存在。有一件事您可能想知道,您的要求允许为北美电话号码生成无效的电话号码。官方标准称为@Tunechi删除内容,使剩下的无用内容无法使用。如果你想掩盖自己的行为,请按“删除”链接删除整个问题。我即将发布的答案与你发布的答案之间的相似性令人恐惧。
makeLast
也不会返回2或3个相同的数字。所以像425-426-8999这样的东西是不可能的。
random.sample
引入了额外的限制。这可以接受吗?要求是“最后4位数字不能全部相同”。我对这一点做了更严格的规定,即“最后4位数字不能彼此相同”。如果你愿意的话,我们可以放宽这个要求,但这样做更容易一行。中间()不能是000?这不是更容易做到吗
second='''.join([str(random.randint(0,8))表示“在范围(3)]”)
?具有未定义时间的缺点(根据,有时会阻塞)。在这种情况下,随机选择错误电话号码的概率仅为0.1%左右(90亿个可能号码中有9990010个错误号码)但一定要记住这一点。
import random

def phn():
    p=list('0000000000')
    p[0] = str(random.randint(1,9))
    for i in [1,2,6,7,8]:
        p[i] = str(random.randint(0,9))
    for i in [3,4]:
        p[i] = str(random.randint(0,8))
    if p[3]==p[4]==0:
        p[5]=str(random.randint(1,8))
    else:
        p[5]=str(random.randint(0,8))
    n = range(10)
    if p[6]==p[7]==p[8]:
        n = (i for i in n if i!=p[6])
    p[9] = str(random.choice(n))
    p = ''.join(p)
    return p[:3] + '-' + p[3:6] + '-' + p[6:]
>>> def gen_phone():
    first = str(random.randint(100,999))
    second = str(random.randint(1,888)).zfill(3)

    last = (str(random.randint(1,9998)).zfill(4))
    while last in ['1111','2222','3333','4444','5555','6666','7777','8888']:
        last = (str(random.randint(1,9998)).zfill(4))

    return '{}-{}-{}'.format(first,second, last)

>>> for _ in xrange(10):
    gen_phone()

'496-251-8419'
'102-665-1932'
'262-624-5025'
'230-459-3242'
'355-131-0243'
'488-001-6828'
'244-539-2369'
'896-547-4539'
'522-406-8256'
'789-373-4240'