【发布时间】:2018-02-27 17:17:18
【问题描述】:
我有一个清单
case_suite_relation_ids= [[1, 2, 3, 1, 2, 3, 1], [1, 1, 1, 2, 2, 2, 3]]
并希望通过以下方式生成字典列表
[{'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}]
我使用了以下代码
keys = ('test_case_id', 'test_suite_id')
list_of_case_suite_relation_rows = [dict(zip(keys, l)) for l in case_suite_relation_ids]
但我得到以下输出
[{'test_case_id': 1, 'test_suite_id': 2}, {'test_case_id': 1, 'test_suite_id': 1}]
有什么办法解决吗?
【问题讨论】:
-
你想得到
[{'test_case_id': case, 'test_suite_id': suite} for case, suite in zip(*case_suite_relation_ids)]吗?
标签: python python-3.x list dictionary