首页 > 解决方案 > 在 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而不仅仅是一个

标签: pythondictionarywhile-loop

解决方案


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}]

推荐阅读