python - 在 WHILE 循环中将“行”添加到字典
问题描述
我在这里遗漏了一些小东西,可以使用指针。我正在尝试生成数据以节省时间,以便通过pymonogo
和其他 pythonic 数据库库在数据库中使用 CRUD 工作。以下是我遇到问题的代码。我想创建一个创建长度字典的函数,n
但我无法弄清楚如何适当地附加字典。如您所见,它只输入生成的列表的最后一项。任何输入都会很棒!
import names
import random
import numpy as np
age_choices = np.arange(18, 90)
gender_choices = ['male', 'female']
salary_choices = np.arange(10000, 200000)
def create_data(n=20):
age_choices = np.arange(18, 90)
gender_choices = ['male', 'female']
salary_choices = np.arange(10000, 200000)
person_values = []
data_dict = {}
unique_id = 0
while unique_id < n:
age = random.choice(age_choices)
gender = random.choice(gender_choices)
salary = random.choice(salary_choices)
person_keys = ['id', 'name', 'gender', 'age', 'salary']
person_values = [unique_id, names.get_full_name(gender), gender, age, salary]
for k, v in zip(person_keys, person_values):
data_dict[k] = v
unique_id += 1
return person_values, data_dict
data_list, data_dict = create_data(5)
print(data_list)
print()
print(data_dict)
当前输出:
[4, 'Anthony Shultz', 'male', 29, 188503] # This is the last item of the list generated in the while loop
{'id': 4, 'name': 'Anthony Shultz', 'gender': 'male', 'age': 29, 'salary': 188503} # This is the "whole" dictionary generated but should have length 5 since n=5
所需的输出应该是一个长度的字典,n
而不仅仅是一个。
解决方案
You should introduce another variable in your function which would be a list or tuple and append each data_dict
to it, every time you create one. You should also create a unique data_dict
in your while
loop, on every iteration. For example (check the lines with comments):
import names
import random
import numpy as np
age_choices = np.arange(18, 90)
gender_choices = ['male', 'female']
salary_choices = np.arange(10000, 200000)
def create_data(n=20):
age_choices = np.arange(18, 90)
gender_choices = ['male', 'female']
salary_choices = np.arange(10000, 200000)
person_values = []
all_data = [] # Make a list which will store all our dictionaries
unique_id = 0
while unique_id < n:
data_dict = {} # Create a dictionary with current values
age = random.choice(age_choices)
gender = random.choice(gender_choices)
salary = random.choice(salary_choices)
person_keys = ['id', 'name', 'gender', 'age', 'salary']
person_values = [unique_id, names.get_full_name(gender), gender, age,
salary]
for k, v in zip(person_keys, person_values):
data_dict[k] = v
all_data.append(data_dict) # Add newly created `data_dict` dictionary to our list
unique_id += 1
return person_values, data_dict, all_data # Return as desired
data_list, data_dict, all_data = create_data(5) # Just as an example
print(data_list)
print()
print(data_dict)
print()
print(all_data) # Print the output
This will result in list of dictionaries, which I assume you want as an output, e.g.:
[{'id': 0, 'name': 'David Medina', 'gender': 'male', 'age': 87, 'salary': 67957}, {'id': 1, 'name': 'Valentina Reese', 'gender': 'female', 'age': 68, 'salary': 132938}, {'id': 2, 'name': 'Laura Franklin', 'gender': 'female', 'age': 84, 'salary': 93839}, {'id': 3, 'name': 'Melita Pierce', 'gender': 'female', 'age': 21, 'salary': 141055}, {'id': 4, 'name': 'Brenda Clay', 'gender': 'female', 'age': 36, 'salary': 94385}]
推荐阅读
- php - show(Model $model) 在 Laravel 7 中不起作用
- c# - 从 Docker 运行的自动扩展 Kafka Consumer
- javascript - 我如何描述这两种价值传递方式之间的区别?
- java - 仅使用 Springboot 和 MongoDB 比较 YYYY-MM 部分日期
- pine-script - Strategy.long 后第二次交叉,Pine 脚本
- c# - 在长时间运行的工作人员服务中创建 HttpClient 对象的建议方法是什么
- bots - 对不和谐机器人的编码感到困惑
- reactjs - 在不渲染父组件 React js 的情况下重新渲染子组件
- javascript - forEach - 在 Redux 中向 action.data 添加键/值 - console.log 有效,但 Redux 状态未更新
- ios - (iOS)无法使用 AVURLAsset 和 AVAsset 阅读器从 RAM 而不是文件读取视频帧