【每日一题0624】随机生成100个不重复的手机号

请用python实现 随机生成100个不重复的手机号

import random


def R100():
    phone_list=[]
    count = 0
    while count<100:
        phone = get_start() + str(get_end()) + str(get_end())
        if phone not in phone_list:
            phone_list.append(phone)
            count = count +1

    print(count)
    print(phone_list)
    return phone_list

def  get_start():
    phone_start = ['137', '138','139','151','152','153','189']
    rand_num = random.randint(0, len(phone_start))
    phone_start_num = phone_start[rand_num-1]
    return  phone_start_num

def get_end():
    rand_num = random.randint(1000, 9999)
    return rand_num


print(R100())

题解1:

import random
import string

def gen_cell_nums():  
    common_prefix = ['130', '131', '132', '133', '135', '136', '138', '139', '176', '180']
    
    result = []
    for i in range(100):
        result.append(random.sample(common_prefix, 1)[0] + ''.join(random.sample(string.digits, 8)))
    return result
    
assert len(set(gen_cell_nums())) == 100

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

from faker import Faker
phone = [fake.phone_number() for x in range(100)]
4 个赞
import faker
def test_phonenum():
    f = faker.Faker(locale='zh-CN')
    for i in range(100):
        print(f.phone_number())  # 随机电话
#coding=utf-8

def fun_a(max_phone):
    for i in range(max_phone):
        yield (str("130%08d"%i))

if __name__ == '__main__':
    max=100
    a=fun_a(max)
    for i in range(max):
        print(next(a))

不重复的手机号

fake=Faker(locale='zh_CN')
    phone = set()
    while len(phone) <100:
        phone.add(fake.phone_number())
        if len(phone)>=100:
            break
    print(phone)

‘’’
请用python实现 随机生成100个不重复的手机号
‘’’
import random

a = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]
b = [‘135’, ‘137’, ‘151’, ‘138’, ‘171’]
y = 0
list =

while True:
first = random.choice(b)
last = random.sample(a, 8)
for x in last:
first = first+x
if first in list:
continue
else:
list.append(first)
y += 1
if y == 100:
print(list)
break

“”"

请用python实现 随机生成100个不重复的手机号

“”"
import random

import yaml

start_num = [134, 135, 136, 137, 138, 139, 150, 151, 155, 156, 158, 177, 179]

def generate_number(target_count: int):
number =
for i in range(target_count):
start_num_choice = str(random.choice(start_num))
anther_number = “”.join([random.choice([“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”]) for _i in range(8)])
number.append(start_num_choice + anther_number)
with open("./phone.yml", “a+”, encoding=“utf-8”) as f:
yaml.safe_dump(start_num_choice + anther_number, f)
yield number

for item in generate_number(100):
print(item)

怎么回复成这种格式的代码呀?我回复的,那些空格键都没了 :scream:

这是markdown的语法格式。你可以尝试用在你的代码前、后分别加上```试试哦。

import random

a = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
b = ['135', '137', '151', '138', '171']
y = 0
list = []

while True:
    first = random.choice(b)
    last = random.sample(a, 8)
    for x in last:
        first = first+x
    if first in list:
        continue
    else:
        list.append(first)
    y += 1
    if y == 100:
        print(list)
        break

image

  import random
  
  
  def phone_number_specified(num):
      i = 0
      dictPhoneNumber = {}
      if type(num) !=int:
          return dictPhoneNumber
      while i < num:
          phoneNumber = phone_number()
          if phoneNumber not in dictPhoneNumber:
              dictPhoneNumber[phoneNumber] = i + 1
              print(phoneNumber)
              i += 1
          else:
              print('重复的手机号' + phoneNumber)
      return dictPhoneNumber
  
  
  def phone_number():
      numOne = "1"
      numTwo = random.choice(['3', '4', '5', '6', '7', '8', '9'])
      result = numOne + numTwo
      for i in range(9):
          numLeft = random.randint(0, 9)
          result = result + str(numLeft)
      return result