新手求助,同时历遍两个元素中所有元素的问题。
mah = []mah.extend(range(2,11))
mah.extend(['J','Q','K','A'])
colour= ['黑桃','红桃','梅花','方块']
想要让mah列表中的所有元素和coloru列表中的所有元素进行组合(形成一副扑克牌)
使用zip函数也不能够实现呀 mah = [*range(2,11), 'J','Q','K','A']
colour= ['黑桃','红桃','梅花','方块']
res = [(i, j) for i in mah for j in colour]
print(res) 这种拼接遍历不就好了?
mah = []
mah.extend(range(2,11))
mah.extend(['J','Q','K','A'])
colour= ['黑桃','红桃','梅花','方块']
result = []
for i in mah:
for j in colour:
result.append(j+str(i))
print(mah)
print(colour)
print(result)
['黑桃', '红桃', '梅花', '方块']
['黑桃2', '红桃2', '梅花2', '方块2', '黑桃3', '红桃3', '梅花3', '方块3', '黑桃4', '红桃4', '梅花4', '方块4', '黑桃5', '红桃5', '梅花5', '方块5', '黑桃6', '红桃6', '梅花6', '方块6', '黑桃7', '红桃7', '梅花7', '方块7', '黑桃8', '红桃8', '梅花8', '方块8', '黑桃9', '红桃9', '梅花9', '方块9', '黑桃10', '红桃10', '梅花10', '方块10', '黑桃J', '
红桃J', '梅花J', '方块J', '黑桃Q', '红桃Q', '梅花Q', '方块Q', '黑桃K', '红桃K', '梅花K', '方块K', '黑桃A', '红桃A', '梅花A', '方块A'] 昨非 发表于 2021-4-17 16:35
这种拼接遍历不就好了?
from itertools import product
print(list(product(colour,mah))) mah = []
mah.extend(range(2,11))
mah.extend(['J','Q','K','A'])
colour= ['黑桃','红桃','梅花','方块']
playing_cards =
print(playing_cards)
hrp 发表于 2021-4-17 16:32
多谢大佬~ 逃兵 发表于 2021-4-17 16:48
拜谢大佬
页:
[1]