我有一个列表,想比较最后一个值是否大于过去 10 个值,但是,我知道有一种比下面的代码更简单的方法来解决这个问题:
list = [1,2,3,4,5,6,7,8,9,10]
if list[-1] > list[-2] and list[-1] > list[-3] and list[-1] > list[-4]: (etc)
print "It's bigger"
如有任何建议,我们将不胜感激!
最佳答案
一种方法是取过去值的最大值并将其与最后一个值进行比较:
>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> if l[-1] > max(l[:-1]):
... print "It's bigger"
...
It's bigger
关于python - 比较python中的列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494726/