在WHILE循环中向字典添加“行”

时间:2020-07-17 11:27:58

标签: python dictionary while-loop

我在这里缺少一些小东西,可以使用一个指针。我试图通过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的字典,而不只是一个

1 个答案:

答案 0 :(得分:2)

您应该在函数中引入另一个变量,该变量将是列表或元组,并在每次创建变量时将每个data_dict附加到变量中。您还应该在每次迭代的data_dict循环中创建一个唯一的while。例如(检查带有注释的行):

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

这将产生字典列表,我假设您希望将其作为输出,例如:

[{'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}]