小编use*_*698的帖子

用于从2个单独列表中的数据填充字典的Python过程

我试图创建一个自动python程序,使用两个单独的列表来创建一个字典,到目前为止我失败了.我有两个排序列表,其中第一个列表中的第n个项目对应于第二个列表中的第n个项目,我想将它们组合成一个字典.

例如,2个列表的子集如下;

name = ['Adam', 'Alfred', 'Amy', 'Andy', 'Bob']
year = [1972, 1968, 1985, 1991, 1989]
Run Code Online (Sandbox Code Playgroud)

我希望我的输出是:

birth_years = {'Adam':1972, 'Alfred':1968, 'Amy':1985, 'Andy':1991, 'Bob':1989}
Run Code Online (Sandbox Code Playgroud)

我试图用for循环来做这个,但我无法让它工作.我感谢任何帮助.

python dictionary

5
推荐指数
1
解决办法
8715
查看次数

当值不唯一时,在python字典中转置值和键

我想在python字典中将键更改为值,但原始字典中的值不是唯一的.

这是我有的:

year_person = {2000: ‘Linda’, 2001: ‘Ron’, 2002: ‘Bruce’, 2003: ‘Linda’, 2004: ‘Bruce’, 2005 ‘Gary’, 2006: ‘Linda’}
Run Code Online (Sandbox Code Playgroud)

这就是我想要改为:

person_year = {‘Linda’: 2000, ‘Ron’: 2001, ‘Bruce’: 2002, ‘Linda’, 2003: ‘Bruce’, 2004 ‘Gary’, 2005: ‘Linda’: 2006}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用for循环转换它时,我每个人只有一对匹配.

python dictionary

2
推荐指数
3
解决办法
9175
查看次数

标签 统计

dictionary ×2

python ×2