如何重新格式化以下元组?

时间:2016-12-14 00:28:38

标签: python string list formatting tuples

我有一大堆这样的元组:

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 个答案:

答案 0 :(得分: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
  4. 中的每个列表迭代此过程

答案 1 :(得分: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)

你可能会发现这个赌注比其他答案更不易碎。

答案 2 :(得分: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')]

答案 3 :(得分: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]