Python “Every Other Element” Idiom [duplicate]

泪湿孤枕 提交于 2019-11-26 08:08:08

问题


This question already has an answer here:

  • Iterating over every two elements in a list 18 answers

I feel like I spend a lot of time writing code in Python, but not enough time creating Pythonic code. Recently I ran into a funny little problem that I thought might have an easy, idiomatic solution. Paraphrasing the original, I needed to collect every sequential pair in a list. For example, given the list [1,2,3,4,5,6], I wanted to compute [(1,2),(3,4),(5,6)].

I came up with a quick solution at the time that looked like translated Java. Revisiting the question, the best I could do was

l = [1,2,3,4,5,6]
[(l[2*x],l[2*x+1]) for x in range(len(l)/2)]

which has the side effect of tossing out the last number in the case that the length isn\'t even.

Is there a more idiomatic approach that I\'m missing, or is this the best I\'m going to get?


回答1:


This will do it a bit more neatly:

>>> data = [1,2,3,4,5,6]
>>> zip(data[0::2], data[1::2])
[(1, 2), (3, 4), (5, 6)]

(but it's arguably less readable if you're not familiar with the "stride" feature of ranges).

Like your code, it discards the last value where you have an odd number of values.




回答2:


The one often-quoted is:

zip(*[iter(l)] * 2)

I prefer this more readable version of the iter solution:

it = iter(l)
list(zip(it, it))
# [(1, 2), (3, 4), (5, 6)]



回答3:


I usually copy the grouper recipe from the itertools documentation into my code for this.

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)



回答4:


How about using the step feature of range():

[(l[n],l[n+1]) for n in range(0,len(l),2)]



回答5:


try this

def pairs(l, n):
    return zip(*[l[i::n] for i in range(n)])

So,

pairs([1, 2, 3, 4], 2) gives

[(1, 2), (3, 4)]



回答6:


The right thing is probably not to compute lists, but to write an iterator->iterator function. This is more generic -- it works on every iterable, and if you want to "freeze" it into a list, you can use the "list()" function.

def groupElements(iterable, n):
    # For your case, you can hardcode n=2, but I wanted the general case here.
    # Also, you do not specify what to do if the 
    # length of the list is not divisible by 2
    # I chose here to drop such elements
    source = iter(iterable)
    while True:
        l = []
        for i in range(n):
            l.append(source.next())
        yield tuple(l)

I'm surprised the itertools module does not already have a function for that -- perhaps a future revision. Until then, feel free to use the version above :)




回答7:


toolz is a well-built library with many functional programming niceties overlooked in itertools. partition solves this (with an option to pad the last entry for lists of odd length)

>>> list(toolz.partition(2, [1,2,3,4,5,6]))
[(1, 2), (3, 4), (5, 6)]



回答8:


If you don't want to lose elements if their number in list is not even try this:

>>> l = [1, 2, 3, 4, 5]
>>> [(l[i],  l[i+1] if i+1 < len(l) else None)  for i in range(0, len(l), 2)]
[(1, 2), (3, 4), (5, None)]


来源:https://stackoverflow.com/questions/2631189/python-every-other-element-idiom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!