从一组列表创建字典,其中键是每个列表的名称,值是列表

时间:2020-05-02 12:29:44

标签: python list dictionary for-loop list-comprehension

我有以下代码:

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

具有以下输出

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]

我想创建字典而不必手动编写变量“ list_of_keys”和“ lists of list”。

我也想使用列表理解而不是for循环,但是当for循环中有'='符号时,我不知道该怎么做。

谢谢

3 个答案:

答案 0 :(得分:1)

您不能避免创建两个列表,但是可以删除循环和字典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)

或更简单的使用dict初始化:

myDict = dict(zip(list_of_keys, list_of_lists))

有关如何初始化的更多详细信息,请参见字典文档。

答案 1 :(得分:1)

您需要以某种方式定义与dict中的每个列表关联的键或“名称”。使用变量名称不是一个好主意,因为它只是每个列表对象的引用。但是,有可能,但强烈建议不要这样做。参见here

如果列表的数量不是很大,则可以直接直接定义字典:

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"]))
]

您实际上是在尝试做什么?或者,您是否想要存储每个员工的词典列表,以便可以像employee[0]['name']->'Mimi'等方式访问它们?

如果列表的数量很大,我将推荐第二种方法,因为代码中的结构清晰明了,并且您无需重复列表的名称,因此具有最干净,最干和最短的代码

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)

答案 2 :(得分:1)

假设您有600个键和值的列表,当您执行程序时,这些数据应作为参数输入或应在脚本中定义。例如,在脚本中定义或可能是由于至少需要定义一个列表,否则需要访问全局变量并获取定义的变量并编写一些过滤方法。

如果定义了list_of_keys,则可以使用eval方法来获取已定义变量的映射对象。

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))

如果要从全局变量中获取定义的变量,则可以从这种方式开始

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