2016-12-14 2 views
2

Ich habe eine große Liste von Tupeln wie folgt aus:Wie können die folgenden Tupel umformatiert werden?

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

Wie kann ich es in Tupeln verwandeln wie folgt ?:

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

Antwort

4

diese Liste Verständnis Versuchen:

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')]] 

Die Tricks sind hier:

  1. Verwenden Sie den dritten Parameter von range, das ist die step Größe
  2. Indizierung der [i+1] -te Begriff, um innerhalb lis
+0

Wenn dies dazu beigetragen, könnten Sie markieren "korrigieren". Vielen Dank! –

+0

Ja das war eigentlich sehr hilfreich. Warten Sie mal! –

1

Sie Tupeln nebeneinander

  • Iterieren diesen Vorgang für jede Liste zu bekommen ' Ich brauche eine pairwise Implementierung, wie die von 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) 
    

    Sie könnten dies eine Wette weniger fragile als die andere Antwort finden.

  • 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]) 
    

    Ergebnis

    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] 
    
    Verwandte Themen