簡體   English   中英

如何從 Python 的列表中刪除引號、逗號和括號?

[英]How can I remove quotes, commas and brackets from lists in Python?

我正在嘗試從 Python 的列表中刪除引號、逗號和方括號。 我是編程新手,不明白。

import itertools
l = ['x', 'o']
card = list(itertools.product(l, repeat= 2))
print(*card, sep = "\n")

#output
('x', 'x')
('x', 'o')
('o', 'x')
('o', 'o')

任何人都可以幫忙嗎? 非常感謝。

這可以通過遍歷列表中的每個項目並打印它來完成。 像這樣 -

import itertools
l = ['x', 'o']
card = list(itertools.product(l, repeat= 2))
for item in card:
    print(*item, end="\n")

Output -

x x 
x o 
o x 
o o 

您的card變量是一個嵌套列表。 為了使列表(和其他可迭代對象)的 output 看起來更平滑,請使用分隔符連接每個項目,例如單個空格:

for item in card:
    print(' '.join(item))

output:

x x
x o
o x
o o

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM