我有一个词典列表和一个列表列表。
dataset = [{'': 0, 'FAILED': 412.698, 'PASSED': 587.301},
{'': 440, 'FAILED': 31.746, 'PASSED': 968.253},
{'': 880, 'FAILED': 253.968, 'PASSED': 746.031},
{'': 1320, 'FAILED': 714.285, 'PASSED': 285.714}]
data_orders = [['', 'FAILED', 'PASSED'],
['', 'FAILED', 'PASSED'],
['', 'PASSED', 'FAILED'],
['', 'PASSED', 'FAILED']]
然后,我想基于像这样的data_orders将dics列表更改为两个元组列表。
FAILED = [(0, 412.698), (440, 471.746), (1626.031, 1879.999), (1605.714, 2319.999)]
PASSED = [(412.698, 999.999), (471.746, 1439.999), (880, 1626.031), (1320, 1605.714)]
如果你看第一个'data_orders'列表,元素是['','FAILED','PASSED']列表'FAILED'的第一个元组是(0,412.968)。 列表'FAILED'的第三个元组与'data_orders的第三个列表相关,因此列表'FAILED'的第三个元组将是(1626.031(880 + 746.031),1879.999(1626.031 + 253.968))。
因为我不擅长英语,所以我担心我的解释有点过于冗长。 任何人都可以告诉我如何编码吗?
答案 0 :(得分:1)
嗯,我不确定你的代码在做什么,但这会提供所需的输出。它有点乱,可以简化我确定,但希望它能帮助你理解你的问题是什么,以及如何以更好的方式解决它!
dataset = [{'': 0, 'FAILED': 412.698, 'PASSED': 587.301},
{'': 440, 'FAILED': 31.746, 'PASSED': 968.253},
{'': 880, 'FAILED': 253.968, 'PASSED': 746.031},
{'': 1320, 'FAILED': 714.285, 'PASSED': 285.714}]
data_orders = [['', 'FAILED', 'PASSED'],
['', 'FAILED', 'PASSED'],
['', 'PASSED', 'FAILED'],
['', 'PASSED', 'FAILED']]
FAILED = []
PASSED = []
for dataset_row,data_order_row in zip(dataset,data_orders):
if data_order_row[1] == 'FAILED':
FAILED.append((dataset_row[''],dataset_row['FAILED']+dataset_row['']))
PASSED.append((dataset_row['FAILED']+dataset_row[''],dataset_row['FAILED']+dataset_row['']+dataset_row['PASSED']))
else:
FAILED.append((dataset_row['']+dataset_row['PASSED'],dataset_row['FAILED']+dataset_row['']+dataset_row['PASSED']))
PASSED.append((dataset_row[''],dataset_row['']+dataset_row['PASSED']))
soln_failed = [(0, 412.698), (440, 471.746), (1626.031, 1879.999), (1605.714, 2319.999)]
soln_passed = [(412.698, 999.999), (471.746, 1439.999), (880, 1626.031), (1320, 1605.714)]
print FAILED == soln_failed
print PASSED == soln_passed