2016-12-14 55 views
2

我的元組這樣的大名單:如何重新格式化以下元組?

lis = [[('iphone', 'ITEM'), 
('69', 'X'), 
('pixel', 'ITEM'), 
('45.91', 'X')], [('xbox', 'ITEM'), 
('8989', 'X'), 
('ps4', 'ITEM'), 
('211.91', 'X')]] 

我怎樣才能將其轉化爲元組這樣?:

lis = [[('iphone', '69'),('pixel', '45.91')], 
     [('xbox', '8989'), ('ps4','211.91')]] 

回答

4

試試這個列表理解:

list_comp = [[(l[i][0],l[i+1][0]) for i in range(0,len(l),2)] for l in lis] 
--------------------------------------------------------------------------- 
Output: 
[[('iphone', '69'), ('pixel', '45.91')], [('xbox', '8989'), ('ps4', '211.91')]] 

的這裏的技巧是:

  1. 使用range第三個參數,這是step大小
  2. 索引[i+1]「相互
  3. 迭代次,以獲得元組任期到明年這個過程中每個列表中lis
+0

如果這幫助下,你可以打 「正確的」。謝謝! –

+0

是的,這實際上是非常有幫助的。等一下! –

1

你」我們需要一個pairwise的實現,就像here那樣。

def pairwise(iterable): 
    "s -> (s0, s1), (s2, s3), (s4, s5), ..." 
    a = iter(iterable) 
    return zip(a, a) 

reformatted = [] 
for device_type in lis: 
    new_device_list = [] 
    for name, number in pairwise(device_type): 
     new_device_list.append((name[0], number[0])) 
    reformatted.append(new_device_list) 

您可能會發現這個賭注不如其他迴應那麼脆弱。

1
lis = [[('iphone', 'ITEM'), 
('69', 'X'), 
('pixel', 'ITEM'), 
('45.91', 'X')], [('xbox', 'ITEM'), 
('8989', 'X'), 
('ps4', 'ITEM'), 
('211.91', 'X')]] 

lis2 = [j[0]for i in lis for j in i ] 
# ['iphone', '69', 'pixel', '45.91', 'xbox', '8989', 'ps4', '211.91'] 
lis3 = [tuple(lis2[i: i+2]) for i in range(0, len(lis2), 2)] 
# [('iphone', '69'), ('pixel', '45.91'), ('xbox', '8989'), ('ps4', '211.91')] 
1
lis = [[('iphone', 'ITEM'), 
('69', 'X'), 
('pixel', 'ITEM'), 
('45.91', 'X')], [('xbox', 'ITEM'), 
('8989', 'X'), 
('ps4', 'ITEM'), 
('211.91', 'X')]] 
print(id(lis),[id(z) for z in lis]) 

# creating a new list by list comprehension instruction, 
# then assigning the new list to the same identifier lis : 
# address of lis is modified 
lis = [[(a,b),(c,d)] for (a,_),(b,_),(c,_),(d,_) in lis] 

print("lis =",lis) 
print(id(lis),[id(z) for z in lis]) 

del lis 
las = [[('iphone', 'ITEM'), 
('69', 'X'), 
('pixel', 'ITEM'), 
('45.91', 'X')], [('xbox', 'ITEM'), 
('8989', 'X'), 
('ps4', 'ITEM'), 
('211.91', 'X')]] 
print(id(las),[id(z) for z in las]) 

# modifiying in-place the list (keeps the same address), 
# but addresses of the elements of the list are changed 
las[:] = [[(a,b),(c,d)] for (a,_),(b,_),(c,_),(d,_) in las] 

print("las =",las) 
print(id(las),[id(z) for z in las]) 

del las 
lus = [[('iphone', 'ITEM'), 
('69', 'X'), 
('pixel', 'ITEM'), 
('45.91', 'X')], [('xbox', 'ITEM'), 
('8989', 'X'), 
('ps4', 'ITEM'), 
('211.91', 'X')]] 

print(id(lus),[id(z) for z in lus]) 

# adresses of the list and its elements remain the same ones 
[ H.extend(((H.pop(0)[0],H.pop(0)[0]),(H.pop(0)[0],H.pop(0)[0]))) 
    for H in lus] 

print("lus =",lus) 
print(id(lus),[id(z) for z in lus]) 

結果

56212872 [55694088, 18466760] 
lis = [[('iphone', '69'), ('pixel', '45.91')], [('xbox', '8989'), ('ps4', '211.91')]] 
55630088 [55693832, 55628552] 
====================== 
55628552 [55630088, 55693832] 
las = [[('iphone', '69'), ('pixel', '45.91')], [('xbox', '8989'), ('ps4', '211.91')]] 
55628552 [55694280, 56212872] 
====================== 
56212872 [55628552, 55694280] 
lus = [[('iphone', '69'), ('pixel', '45.91')], [('xbox', '8989'), ('ps4', '211.91')]] 
56212872 [55628552, 55694280]