python - 比较python中的列表元素

标签 python list compare element

我有一个列表,想比较最后一个值是否大于过去 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/

相关文章:

python - 以数据帧格式打印列表

python - 如何将特殊格式的文本组织到列表中(在 python 上)

python - 使用列表理解绘制存储在字典列表中的数据

html - 你知道网页外观比较器吗?

python - 为什么python中会出现这个AttributeError?

python - 有没有办法让这个 numpy 操作更快?

c++ - 当 List<List<X>> 时,模板在 C++ 中删除

java - jpa 标准比较两个字符串而不考虑空格

python - 如何比较 python 函数的性能?

python - 如果调用特定子命令,则执行不同的父命令操作?