for i, j in cards:
# cards = a list containing list of cards - RANDOM OUTPUTS, more pairs can be added to the list depending on the number that the user puts in
print(i)
print(j)
print("\t")
如何使输出变为:
TS 6S JS
AH 5S AS
而不是:
TS
AH
6S
5S
JS
AS
我发布了一个与此相似的问题,但我不够具体,而且编辑太晚了。提前道歉
编辑 - '卡'代码:
deck = deck_create()
def deal_cards(deck, num_players):
cards= []
for i in range(num_players):
hands.append([deck.pop(), deck.pop()])
return hands
答案 0 :(得分:4)
您可能会有点想象,并使用zip(*)
来转置cards
。然后打印很简单,如下:
cards=[("TS", "AH"), ("6S", "5S"), ("JS", "AS")]
for round in zip(*cards):
print('\t'.join(round))
输出:
TS 6S JS
AH 5S AS
答案 1 :(得分:0)
简单,运行循环两次。字符输出仅允许您可靠地一次操作一行。要以垂直列打印,需要在两次传递中剥离数据。与仅运行两次相比,使用IF语句,循环内部会减慢处理速度。 Python可以优化这种代码,因为它不需要分支预测。
{{1}}
答案 2 :(得分:0)
我认为你需要,
for i in range(len(cards[0])):
for items in cards:
if items[i] in cards[-1]:
print(items[i],end="\n")
else:
print(items[i],end="\t")