first_names = ['张', '王', '李', '刘', '赵', '陈', '周', '钱', "孙", "吴", "曾", "冯", "陶", "将", "黄", "胡"] s_names = ['云', '风', '山', '河', '林', '羽', '行', "雪", '月', '雨', '冰', '雷', '鞥', '时', '文', '代'] l_names = ['雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白'] genders = ['MALE', 'FEMALE'] id_num = random.randint(100000, 999999) # 生成随机的姓名和性别 name = random.choice(first_names) + random.choice(s_names) + random.choice(l_names) gender = random.choice(genders) # 生成随机的电话号码 phone = '1' + str(random.randint(3, 9)) + ''.join(str(random.randint(0, 9)) for _ in range(9))
时间: 2023-08-20 15:08:13 浏览: 34
这段代码是用于生成随机的姓名、性别和电话号码。首先定义了一些常用的姓氏、名字和性别。然后使用random模块生成随机的ID号码。接着从姓氏、名字和性别列表中随机选择一个生成姓名,再随机生成一个性别。最后生成一个随机的电话号码,以1开头,后面跟随10位数字。
该代码可能用于模拟生成一些虚假的个人信息数据,用于测试或其他需要随机数据的场景。
相关问题
优化:def generate_data(add_user): # 学/工号 id_num = random.randint(100000, 999999) # 姓名 first_names = ['张', '王', '李', '刘', '赵', '陈', '周', '钱', "孙", "吴", "曾", "冯", "陶", "将", "黄", "胡", "朱", "杨", "江", "何"] s_names = ['云', '风', '山', '河', '林', '羽', '行', "雪", '月', '雨', '冰', '雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白'] l_names = ['雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白'] name = random.choice(first_names) + random.choice(s_names) + random.choice(l_names) # 性别 gender = random.choice(['男', '女']) # 身份 identity = random.choice(["学生"]) # 部门 department = random.choice( ["20230608094700005793941100036910"]) # 手机号 phone_num = '1' + str(random.randint(3, 9)) + ''.join(str(random.randint(0, 9)) for _ in range(9)) id_types = ['身份证', '护照', '驾驶证', '学生证', '工作证'] if add_user: id_type = random.choice(id_types) if id_type == '身份证': id_num = ''.join(str(random.randint(0, 9)) for _ in range(18)) elif id_type == '护照': id_num = ''.join(str(random.randint(0, 9)) for _ in range(9)) elif id_type == '驾驶证': id_num = ''.join(str(random.randint(0, 9)) for _ in range(12)) elif id_type == '学生证': id_num = ''.join(str(random.randint(0, 9)) for _ in range(10)) else: id_num = ''.join(str(random.randint(0, 9)) for _ in range(8)) return [id_num, name, gender, phone_num, identity, department, phone_num, id_type, id_num] else: return [name, gender, identity, department, phone_num]
以下是对该函数的一些优化建议:
1. 减少重复代码:函数中有两行代码都生成了手机号,可以将其中一行删掉。
2. 函数参数命名:`add_user` 这个参数名不太符合语义,建议改成 `include_id_num` 或类似的名称。
3. 减少代码行数:可以将 `id_num` 的生成放到 `if add_user:` 的条件判断中,从而减少重复代码。
4. 使用 f-string:可以使用 f-string 来简化字符串拼接。
5. 使用列表推导式:可以使用列表推导式来生成 `first_names`、`s_names` 和 `l_names` 列表。
下面是修改后的代码:
```python
import random
def generate_data(include_id_num):
# 姓名
first_names = ['张', '王', '李', '刘', '赵', '陈', '周', '钱', "孙", "吴", "曾", "冯", "陶", "将", "黄", "胡", "朱", "杨", "江", "何"]
s_names = ['云', '风', '山', '河', '林', '羽', '行', "雪", '月', '雨', '冰', '雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白']
l_names = ['雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白']
name = random.choice(first_names) + random.choice(s_names) + random.choice(l_names)
# 性别
gender = random.choice(['男', '女'])
# 身份
identity = random.choice(["学生"])
# 部门
department = random.choice(["20230608094700005793941100036910"])
# 手机号
phone_num = '1' + str(random.randint(3, 9)) + ''.join(str(random.randint(0, 9)) for _ in range(9))
if include_id_num:
# 学/工号
id_num = random.randint(100000, 999999)
# 证件类型和号码
id_types = ['身份证', '护照', '驾驶证', '学生证', '工作证']
id_type = random.choice(id_types)
if id_type == '身份证':
id_num = ''.join(str(random.randint(0, 9)) for _ in range(18))
elif id_type == '护照':
id_num = ''.join(str(random.randint(0, 9)) for _ in range(9))
elif id_type == '驾驶证':
id_num = ''.join(str(random.randint(0, 9)) for _ in range(12))
elif id_type == '学生证':
id_num = ''.join(str(random.randint(0, 9)) for _ in range(10))
else:
id_num = ''.join(str(random.randint(0, 9)) for _ in range(8))
return [str(id_num), name, gender, phone_num, identity, department, id_type, id_num, phone_num]
else:
return [name, gender, identity, department, phone_num]
```
加证件类型,证件号:def generate_data(add_user): # 学/工号 id_num = random.randint(100000, 999999) # 姓名 first_names = ['张', '王', '李', '刘', '赵', '陈', '周', '钱', "孙", "吴", "曾", "冯", "陶", "将", "黄", "胡", "朱", "杨", "江", "何"] s_names = ['云', '风', '山', '河', '林', '羽', '行', "雪", '月', '雨', '冰', '雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白'] l_names = ['雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白'] name = random.choice(first_names) + random.choice(s_names) + random.choice(l_names) # 性别 gender = random.choice(['男', '女']) # 身份 identity = random.choice(["学生"]) # 部门 department = random.choice( ["20230608094700005793941100036910"]) # 手机号 phone_num = '1' + str(random.randint(3, 9)) + ''.join(str(random.randint(0, 9)) for _ in range(9)) if add_user: return [id_num, name, gender, identity, department, phone_num] else: return
好的,我来帮您修改一下:
```python
import random
def generate_data(add_user):
# 学/工号
id_num = random.randint(100000, 999999)
# 姓名
first_names = ['张', '王', '李', '刘', '赵', '陈', '周', '钱', "孙", "吴", "曾", "冯", "陶", "将", "黄", "胡", "朱", "杨", "江", "何"]
s_names = ['云', '风', '山', '河', '林', '羽', '行', "雪", '月', '雨', '冰', '雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白']
l_names = ['雷', '鞥', '时', '文', '代', '格', '台', "发", '方', '欧', '白']
name = random.choice(first_names) + random.choice(s_names) + random.choice(l_names)
# 性别
gender = random.choice(['男', '女'])
# 证件类型
id_type = random.choice(["身份证", "护照", "驾驶证"])
# 证件号
id_number = ''.join(str(random.randint(0, 9)) for _ in range(18)) if id_type == "身份证" else ''.join(str(random.randint(0, 9)) for _ in range(9))
# 身份
identity = random.choice(["学生"])
# 部门
department = random.choice(["20230608094700005793941100036910"])
# 手机号
phone_num = '1' + str(random.randint(3, 9)) + ''.join(str(random.randint(0, 9)) for _ in range(9))
if add_user:
return [id_num, name, gender, id_type, id_number, identity, department, phone_num]
else:
return
```
其中,我添加了 `id_type` 和 `id_number` 两个变量用于生成证件类型和证件号,并将它们添加到了返回的列表中,以便您使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)