2016-05-03 56 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