简体   繁体   中英

adding 'rows' to dictionary in a WHILE loop

I am missing something small here and could use a pointer. I am trying to generate data to save time for my work with CRUD work in a database via pymonogo and other pythonic database libraries. Below is the code that I am having trouble with. I would like to create a function which creates a dictionary of length n but I cannot figure out how to append the dictionary appropriately. As you can see, it only enters in the last item of the list generated. Any input would be great!

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)

current outputs:

[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

The desired out put should be a dictionary of length n not just one .

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, eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM