Jak scalić listy w listę krotek?

Jakie jest podejście Pythoniczne, aby osiągnąć następujące?

# Original lists:

list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]

# List of tuples from 'list_a' and 'list_b':

list_c = [(1,5), (2,6), (3,7), (4,8)]

Każdy człon list_c jest krotką, której pierwszy człon pochodzi z list_a, a drugi z list_b.

Author: martineau, 2010-03-09

8 answers

>>> list_a = [1, 2, 3, 4]
>>> list_b = [5, 6, 7, 8]
>>> zip(list_a, list_b)
[(1, 5), (2, 6), (3, 7), (4, 8)]
 304
Author: YOU,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-27 15:13:36

W Pythonie 3.0 zip zwraca obiekt zip. Możesz uzyskać z niej listę wywołując list(zip(a, b)).

 115
Author: Lodewijk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-27 15:13:45

Szukasz wbudowanej funkcji zip .

 8
Author: Mizipzor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-09 07:55:50

Możesz użyć mapy lambda

a = [2,3,4]
b = [5,6,7]
c = map(lambda x,y:(x,y),a,b)

To również zadziała, jeśli długość oryginalnych List nie pasuje

 8
Author: Dark Knight,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-04 05:49:00

Wiem, że to stare pytanie i już na nie odpowiedziałam, ale z jakiegoś powodu nadal chcę opublikować to alternatywne rozwiązanie. Wiem, że łatwo jest po prostu dowiedzieć się, która wbudowana funkcja robi" magię", której potrzebujesz, ale nie zaszkodzi wiedzieć, że możesz to zrobić sam.

>>> list_1 = ['Ace', 'King']
>>> list_2 = ['Spades', 'Clubs', 'Diamonds']
>>> deck = []
>>> for i in range(max((len(list_1),len(list_2)))):
        while True:
            try:
                card = (list_1[i],list_2[i])
            except IndexError:
                if len(list_1)>len(list_2):
                    list_2.append('')
                    card = (list_1[i],list_2[i])
                elif len(list_1)<len(list_2):
                    list_1.append('')
                    card = (list_1[i], list_2[i])
                continue
            deck.append(card)
            break
>>>
>>> #and the result should be:
>>> print deck
>>> [('Ace', 'Spades'), ('King', 'Clubs'), ('', 'Diamonds')]
 5
Author: Kruger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-17 12:58:17

Wyjście, które pokazałeś w instrukcji problemu, nie jest krotką, ale listą

list_c = [(1,5), (2,6), (3,7), (4,8)]

Sprawdź

type(list_c)

Biorąc pod uwagę, że chcesz, aby wynik był krotką poza list_a i list_b, wykonaj

tuple(zip(list_a,list_b)) 
 4
Author: cyborg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-12 14:52:22

Jedna alternatywa bez użycia zip:

list_c = [(p1, p2) for idx1, p1 in enumerate(list_a) for idx2, p2 in enumerate(list_b) if idx1==idx2]

W przypadku, gdy chcemy uzyskać nie tylko krotki 1st z 1st, 2nd z 2nd... ale wszystkie możliwe kombinacje 2 List, które byłyby zrobione z

list_d = [(p1, p2) for p1 in list_a for p2 in list_b]
 0
Author: J0ANMM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-15 13:57:41

Nie jestem pewien, czy to sposób pythoniczny, czy nie, ale wydaje się to proste, jeśli obie listy mają taką samą liczbę elementów:

list_a = [1, 2, 3, 4]

list_b = [5, 6, 7, 8]

list_c=[(list_a[i],list_b[i]) for i in range(0,len(list_a))]
 0
Author: Vipin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 09:01:28