如何比较同一列表中的两个相邻项目 - Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14012562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two adjacent items in the same list - Python
提问by Ricochet_Bunny
I am searching for a way to compare two adjacent items in a list, eg. comparing which has a higher value, and then I will sort them accordingly. It is a list the user will be inputting, so it is not a case of just
if l[1] > l[2], as I will not know the length of the list, so
I will need a general statement for use in a for loop.
我正在寻找一种方法来比较列表中的两个相邻项目,例如。比较哪个具有更高的价值,然后我会相应地对它们进行排序。它是用户将要输入的列表,所以它不是 just 的情况
if l[1] > l[2],因为我不知道列表的长度,所以我需要在 for 循环中使用的一般语句。
I had the idea of having something akin to
for i in l:
if x > i[index of x + 1]but do not know how to find the index of the variable.
Any help appreciated, Thank you
我有类似的想法,
for i in l:
if x > i[index of x + 1]但不知道如何找到变量的索引。任何帮助表示赞赏,谢谢
EDIT: I am aware of the built in sort function, but just wanted to practice coding and algorithm-writing by creating my own :)
编辑:我知道内置的排序函数,但只是想通过创建自己的来练习编码和算法编写:)
回答by ThiefMaster
The quick-and-ugly solution would be this (don't use it!):
快速而丑陋的解决方案是这样的(不要使用它!):
for i, item in enumerate(lst):
# here you can use lst[i + 1] as long as i + 1 < len(lst)
However, do notimplement list sorting by yourself! Use .sort()to sort in-place or sorted()if you want to create a new list instead. There is a really good guideon how to sort things on the python website.
但是,不要自己实现列表排序!用于.sort()就地排序,或者sorted()如果您想创建一个新列表。关于如何在 python 网站上对事物进行排序,有一个非常好的指南。
If that's not your intention.. instead of the loop I posted above there's also a much nicer way to iterate over chunks from a list in another SO question:
如果这不是您的意图.. 而不是我上面发布的循环,还有一种更好的方法来迭代另一个 SO 问题中的列表中的块:
import itertools
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
You use to like this:
你曾经喜欢这样:
for x, y in grouper(2, lst):
# do whatever. in case of an odd element count y is None in the last iteration
回答by Ashwini Chaudhary
回答by Somesh
you can also use inbuilt reduce function
您还可以使用内置的减少功能
e.g. :
例如:
l = [1,2,3,4,5,6,7]
def my_function(a,b):
# your comparison between a and b
# return or print values or what ever you want to do based on the comparison
reduce(my_function, l)
reduce will automatically take care of i and i + 1.
reduce 会自动处理 i 和 i + 1。
Hope it helps. :)
希望能帮助到你。:)
回答by Lu.nemec
there is built in function cmp, which you can use for the comparison
有内置函数cmp,您可以使用它进行比较
I needed to check if all items in list are identical, so I did this:
我需要检查列表中的所有项目是否都相同,所以我这样做了:
def compare(x, y):
if x == y:
return x
return False
reduce(compare, my_list)
When you run this with say [1,1,1,1,1,1], it prints 1, when one of numbers doesn't match, it returns False .. simple
当你用 [1,1,1,1,1,1] 运行它时,它打印 1,当其中一个数字不匹配时,它返回 False .. 简单

