优化: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]
时间: 2024-01-02 19:04:50 浏览: 76
以下是对该函数的一些优化建议:
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]
```
阅读全文