简体   繁体   中英

Creating a dictionary from a group of lists, where the keys are the name of each list and the values are the lists

I have the following code:

names = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email = [names[i] + '_' + surnames[i] + '@email.com' for i in range(10)]
salary = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

list_of_keys = ['names', 'surnames', 'email', 'salary', 'gender', 'age']
list_of_lists = [names, surnames, email, salary, gender, age]

myDict = {}
for i in range(6):
    myDict[list_of_keys[i]] = list_of_lists[i]

for i in myDict:
    print(i,': ', myDict[i])

That has the following output

names :  ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames :  ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email :  ['Mimi_Perez@email.com', 'Monique_Gomez@email.com', 'Derick_Sanchez@email.com', 'Pierre_Iglesias@email.com', 'Sara_Casado@email.com', 'Marti_Mata@email.com', 'Isabel_Li@email.com', 'Elicia_Perez@email.com', 'Dani_Li@email.com', 'Bell_Gomez@email.com']
salary :  [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender :  ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age :  [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

I would like to create the dictionary without having to manually write the variables 'list_of_keys' and 'list of lists'.

I also would like to use a list comprehension instead of a for loop, but I dont know how to do it when there is an '=' sign in the for loop.

Thank you

You can't avoid the creation of the two lists but you can remove the loop and the dictionary init:

names = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email = [names[i] + '_' + surnames[i] + '@email.com' for i in range(10)]
salary = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

list_of_keys = ['names', 'surnames', 'email', 'salary', 'gender', 'age']
list_of_lists = [names, surnames, email, salary, gender, age]

# Dictionary comprehension
myDict = {k: v for (k, v) in zip(list_of_keys, list_of_lists)}

print(myDict)

Or even simpler with the dict init:

myDict = dict(zip(list_of_keys, list_of_lists))

See dictionary documentation fr more details about how to init.

You somehow need to define the key or 'name' that is associated to each list in your dict. Using the name of the variable is not a good idea, as it is only a reference to each list object. However, it is possible, but highly discouraged. See here .

If the number of lists is not very large, you could simply define your dict directly:

names = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email = [names[i] + '_' + surnames[i] + '@email.com' for i in range(len(names))]
salary = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

my_data = {
    "names": names,
    "surnames": surnames,
    "email": email,
    "salary": salary,
    "gender": gender,
    "age": age,
}

# Or simply define the lists inside the dictionary


my_data = {
    "names": ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell'],
    "surnames": ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez'],
    "salary": [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000],
    "gender": ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F'],
    "age": [31, 33, 30, 31, 34, 34, 31, 31, 32, 30],
}
# Email depends on the size of the other lists, so we add it afterwards
my_data["email"] = [
    names[i] + "_" + surnames[i] + "@email.com" for i in range(len(my_data["names"]))
]

Is that what you are actually trying to do? Or do you want a list of dicts to store each employee so that you can access them like employee[0]['name'] -> 'Mimi', etc.?

If the number of lists is large, I would recommend the second approach, as the structure is clear from the code, and you don't need to repeat the names of the lists and therefore have the cleanest, DRYest and shortest code.

my_data = {}
my_data["names"] = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
my_data["surnames"] = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
my_data["salary"] = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
my_data["gender"] = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
my_data["age"] = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]
my_data["email"] = [
    my_data["names"][i] + "_" + my_data["surnames"][i] + "@email.com" for i in range(len(my_data["names"]))
]

import pandas
df = pandas.DataFrame.from_dict(my_data)

Let's say you have 600 list of keys and values,when you execute the program those data should be come as a parameters or it should be defined in the script.For example it is defined in the script or it could be results from a database.Somehow you need to define at least one list or otherwise you need to access the globals and get the defined variables and write some filtering method.

If you defined the list_of_keys ,then you can use eval method to get the mapping object of the defined variables.

list_of_keys = ['names', 'surnames', 'email', 'salary', 'gender', 'age']
list_of_lists = [eval(i) for i in list_of_keys]

result = dict(zip(list_of_keys, list_of_lists))

If you want to get defined variables from the globals,you can start from this way

g_keys = [item for item in dir() if (not item.startswith("__") and not item.startswith("_"))]
for k in ks:
    if isinstance(eval(k),list):
        print(k)
        // you have to find a way to remove unwanted values

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