2016-05-03 43 views
1

所以我没有什么问题。 我有2个位置和运动日的结果:范围函数来计算结果

location1 = [401, 604, 547, 700, 722, 845, 621, 490, 800, 700] 
location2 = [900, 0, 333, 803, 838, 400, 467, 488, 432, 700] 

,我要得到的结果是这样,只有最好的显示。最后还有这些结果的总和。 最终的结果应该是这样的:

[900, 604, 547, 803, 845, 621, 490, 800, 700] 
Sum: 7148 

我得到的位置与最佳总体得分和金额,而不是从每个最好的结果。任何人有想法?

+0

['zip'](https://docs.python.org/2/library/functions.html#zip)两者的位置列出在一起并找到['max'](HTTPS ://docs.python.org/2/library/functions.html#max)与列表理解或['map'](https://docs.python.org/2/library/functions.html #map),然后['sum'](https://docs.python.org/2/library/functions.html#sum)它们一起。 –

回答

3
best_results = [max(x,y) for x,y in zip(location1, location2)] 
2

试试这个:

max_results = [max(item) for item in zip(location1,location2)] 
total = sum(max_results) 

或者只是为了运动:

max_results = [max(location1[index],location2[index]) for index in range(0, len(location1))] 
1

这也将正常工作。

best_results = list(map(lambda x: max(location1[x], location2[x]), range(len(location1)))) 
total = sum(best_results) 
+1

“这**将**也有效”将会更准确。 –

0
results = [max(x,y) for x,y in zip(location1, location2)] 
total = sum(results) 
+0

虽然此代码可能会回答这个问题,但提供关于为什么和/或如何回答问题的附加背景将显着提高其长期价值。请[编辑]你的答案,添加一些解释。 – CodeMouse92