小编Dee*_*ran的帖子

使用包含重复值的列表在python中生成字典列表

我有一份清单

case_suite_relation_ids= [[1, 2, 3, 1, 2, 3, 1], [1, 1, 1, 2, 2, 2, 3]]
Run Code Online (Sandbox Code Playgroud)

并希望以下列方式生成字典列表

[{'test_case_id': 1, 'test_suite_id': 1}, {'test_case_id': 2, 'test_suite_id': 1},{'test_case_id': 3, 'test_suite_id': 1}, {'test_case_id': 1, 'test_suite_id': 2}, {'test_case_id': 2, 'test_suite_id': 2}, {'test_case_id': 3, 'test_suite_id': 2}, {'test_case_id': 1, 'test_suite_id': 3}]
Run Code Online (Sandbox Code Playgroud)

我使用了以下代码

keys = ('test_case_id', 'test_suite_id')
list_of_case_suite_relation_rows = [dict(zip(keys, l)) for l in case_suite_relation_ids]
Run Code Online (Sandbox Code Playgroud)

但我得到以下输出

[{'test_case_id': 1, 'test_suite_id': 2}, {'test_case_id': 1, 'test_suite_id': 1}]
Run Code Online (Sandbox Code Playgroud)

任何解决方案如何解决?

python dictionary list python-3.x

2
推荐指数
1
解决办法
108
查看次数

标签 统计

dictionary ×1

list ×1

python ×1

python-3.x ×1