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

问题描述 投票:0回答:1

我有以下代码。

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 :  ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
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'和'list of lists'。

我还想用列表理解代替for循环,但我不知道如何在for循环中使用'='符号。

谢谢你的帮助

python list dictionary for-loop list-comprehension
1个回答
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 init:

myDict = dict(zip(list_of_keys, list_of_lists))

更多关于如何init的细节请参见字典文档。


1
投票

假设你有600个键和值的列表,当你执行程序时,这些数据应该是作为一个参数来的,或者应该在脚本中定义,例如它是在脚本中定义的,或者它可能是来自数据库的结果,你需要以某种方式定义至少一个列表,否则你需要访问globals并获得定义的变量,并写一些过滤方法。

如果你定义了 list_of_keys那么你可以使用 评价 方法来获取定义变量的映射对象。

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

如果你想从globals中获取定义的变量,你可以从下面的方法开始。

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

1
投票

你需要以某种方式定义与你的dict中的每个列表相关联的键或 "名称"。使用变量的名称不是一个好主意,因为它只是一个 参考 到每个列表对象。然而,这是可能的,但非常不鼓励。参见 此处.

如果列表的数量不是很多,你可以直接定义你的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'] ->'Mimi'等?

如果列表数量较多,我建议采用第二种方法,因为从代码上看,结构很清晰,不需要重复列表名称,因此代码最简洁、最DRY、最短。

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)
© www.soinside.com 2019 - 2024. All rights reserved.