python - 如何在字典中的列表列表上迭代一个键?

标签 python dictionary nested

<分区>

我现在有点难过。我想获取一个键列表并将其遍历列表列表

tests = ['test 1', 'test 2', 'test 3']
scores = [[90, 70, 60], [40, 50, 100], [60, 65, 90], [30, 61, 67], 
[80, 79, 83], [70, 97, 100]]

预期结果:

我想返回一个显示以下内容的字典:

'test 1': 90,'test 2' : 70, 'test 3': 60, 'test 1': 40, 'test 2': 50, 
'test 3': 100... 'test 1' : 70, 'test 2' : 97, 'test 3':100

测试 1:得分 1

测试 2:得分 2

测试 3:得分 3

最佳答案

dictzip一起使用:

[dict(zip(tests, score)) for score in scores]

输出:

[{'test 1': 90, 'test 2': 70, 'test 3': 60},
 {'test 1': 40, 'test 2': 50, 'test 3': 100},
 {'test 1': 60, 'test 2': 65, 'test 3': 90},
 {'test 1': 30, 'test 2': 61, 'test 3': 67},
 {'test 1': 80, 'test 2': 79, 'test 3': 83},
 {'test 1': 70, 'test 2': 97, 'test 3': 100}]

关于python - 如何在字典中的列表列表上迭代一个键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57750758/

相关文章:

python - 比较 Python 中的 3 个列表

python - 我不确定如何让我的字典打印我想要的信息。 Python 3.1

sql-server - 在内部 SELECT 语句中分配变量

python:继承或组合

python - 正在寻找可能更好的方法来使用 Glom 获取嵌套数据?

regex - 使用正则表达式匹配两个字符串之间的字符串

python - SimpleBlobDetector - 隔离与周围不同的 Blob

python - Matplotlib 步进图旋转

python - 更改数据框中一组条目的数据类型

python - 如何在字典中添加字典