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

2024-06-01 09:38:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

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]

我想创建字典,而不必手动编写变量“listofkeys”和“listoflists”

我也希望使用列表理解而不是for循环,但是当for循环中有一个“=”符号时,我不知道怎么做

多谢各位


Tags: ofcomforagenamesemailligender
3条回答

假设您有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

您无法避免创建两个列表,但可以删除循环和字典初始化:

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)

或者使用dictinit更简单:

myDict = dict(zip(list_of_keys, list_of_lists))

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

您需要在dict中定义与每个列表关联的键或“名称”。使用变量名称不是一个好主意,因为它只是对每个列表对象的引用。然而,这是可能的,但非常令人沮丧。见here

如果列表数量不是很大,您可以直接定义dict:

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

这就是你真正想做的吗?或者,您是否希望存储每个员工的dict列表,以便您可以像employee[0]['name']->;一样访问这些dict咪咪,等等

如果列表的数量很大,我建议使用第二种方法,因为代码中的结构很清晰,不需要重复列表的名称,因此代码最干净、最干、最短

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)

相关问题 更多 >