简体   繁体   English

元组列表中的2个项目

[英]2 items from a list of tuples

I have a list of tuples: 我有一个元组列表:

Items = [(4, 2), (1, 1), (2, 4), (8, 6), (11, 4), (10, 2), (7, 3), (6, 1)]

and I want to get it like so in a for loop: 我希望在for循环中得到它:

NewItems = [[(4, 2), (1, 1)], 
            [(2, 4), (8, 6)],
            [(11, 4), (10, 2)],
            [(7, 3), (6, 1)]]

I did this: 我这样做了:

NewItems = []
while len(Items) > 0:
    NewItems.append([Items[0], Items[1]])
    del Items[0:2]
print NewItems

I don't think it's the best way, since I'm deleting Items variables. 我不认为这是最好的方法,因为我正在删除Items变量。 Then I tried to do this: 然后我试着这样做:

newList = iter(Items)
NewItems = []
for a, b in zip(newList, newList):
    NewItems.append([a, b])
print NewItems

but that's merging the tuples. 但那是合并元组的。

Is there any better solution that can do the same thing? 有没有更好的解决方案可以做同样的事情?

If you don't mind tuples of tuples instead of the inner lists, just zip it up: 如果你不介意元组的元组而不是内部列表,只需将其压缩:

>>> zip(Items[0::2], Items[1::2])
[((4, 2), (1, 1)), ((2, 4), (8, 6)), ((11, 4), (10, 2)), ((7, 3), (6, 1))]

You can use a list comprehension where you zip the items in pairs. 您可以使用列表推导来成对压缩项目。

Items = [(4, 2), (1, 1), (2, 4), (8, 6), (11, 4), (10, 2), (7, 3), (6, 1)]
New_Items = [list(pair) for pair in zip(Items[::2], Items[1::2])]

>>> New_Items
[[(4, 2), (1, 1)], [(2, 4), (8, 6)], [(11, 4), (10, 2)], [(7, 3), (6, 1)]]
>>> items = [(4,2),(1,1),(2,4),(8,6),(11,4),(10,2),(7,3),(6,1)]
>>> new_items = [items[i:i+2] for i in range(0, len(items), 2)] 
>>> new_items
[[(4, 2), (1, 1)], [(2, 4), (8, 6)], [(11, 4), (10, 2)], [(7, 3), (6, 1)]]

You can do it with the while loop like this. 你可以像这样使用while循环。

>>> new_items = []
>>> while items:
...     new_items.append((items.pop(0), items.pop(0)))
... 
>>> new_items
[((4, 2), (1, 1)), ((2, 4), (8, 6)), ((11, 4), (10, 2)), ((7, 3), (6, 1))]

However this is destructive to items and isn't very efficient due to using pop(0) which is O(n) 然而,这对items是破坏性的,并且由于使用pop(0)是O(n)而不是非常有效

How about 怎么样

Items = [(4,2),(1,1),(2,4),(8,6),(11,4),(10,2),(7,3),(6,1)]

NewItems = []
j =0   
for i in xrange(1,len(Items)-1): 
    if j+1 > len(Items):
        break
    NewItems.append([Items[j],Items[j+1]])
    j += 2

print NewItems
from time import time

Items = [(4,2),(1,1),(2,4),(8,6),(11,4),(10,2),(7,3),(6,1)]
NewItems = []
start_time = time()
while len(Items) > 0 :
    NewItems.append([Items[0],Items[1]])
    del Items[0:2]
print NewItems
print time() - start_time

Items = [(4,2),(1,1),(2,4),(8,6),(11,4),(10,2),(7,3),(6,1)]
NewItems = []
start_time = time()
for i in range(len(Items) / 2):
    NewItems.append([Items[2*i],Items[2*i + 1]])
print NewItems
print time() - start_time

With some timing so you can verify that the second solution is faster 通过一些时间安排,您可以验证第二个解决方案是否更快

Yet another approach, but probably not the prettiest. 还有另一种方法,但可能不是最漂亮的。

x = []
i = iter(Items)

while True:
    try:
        x.append([i.next(), i.next()])
    except StopIteration:
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM