将一个int列表和一个字符串列表放入一个元组列表中(int,string)

时间:2015-09-24 00:25:12

标签: string list python-3.x int tuples

我有一个列表[1,2,3,...,13]和一个列表['clubs','hearts,'spades','diamonds']。如何制作看起来像...的元组列表? [(1,hearts),(1,clubs),(1,spades),(1,diamonds),(2,hearts)...等等?

我尝试使用列表理解但无法弄明白。

2 个答案:

答案 0 :(得分:1)

来自Python docs

rank = [1, 2, 3]
suit = ['clubs', 'hearts', 'spades']
card = [(a, b) for a in rank for b in suit]

答案 1 :(得分:0)

使用product from itertools

的另一种解决方案
from itertools import product
rank = range(1,14)
suit = ['clubs','hearts','spades','diamonds']
print( list(product(rank, suit)) )

你得到:

[(1, 'clubs'), (1, 'hearts'), (1, 'spades'), (1, 'diamonds'), (2, 'clubs'), (2, 'hearts'), (2, 'spades'), (2, 'diamonds'), (3, 'clubs'), (3, 'hearts'), (3, 'spades'), (3, 'diamonds'), (4, 'clubs'), (4, 'hearts'), (4, 'spades'), (4, 'diamonds'), (5, 'clubs'), (5, 'hearts'), (5, 'spades'), (5, 'diamonds'), (6, 'clubs'), (6, 'hearts'), (6, 'spades'), (6, 'diamonds'), (7, 'clubs'), (7, 'hearts'), (7, 'spades'), (7, 'diamonds'), (8, 'clubs'), (8, 'hearts'), (8, 'spades'), (8, 'diamonds'), (9, 'clubs'), (9, 'hearts'), (9, 'spades'), (9, 'diamonds'), (10, 'clubs'), (10, 'hearts'), (10, 'spades'), (10, 'diamonds'), (11, 'clubs'), (11, 'hearts'), (11, 'spades'), (11, 'diamonds'), (12, 'clubs'), (12, 'hearts'), (12, 'spades'), (12, 'diamonds'), (13, 'clubs'), (13, 'hearts'), (13, 'spades'), (13, 'diamonds')]