简体   繁体   中英

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

I am trying to remove the quotes, commas and brackets from lists in Python. I am new to programming and don't get it.

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')

Can anyone help? Thanks a lot.

This can be done by iterating through each item in the list and printing it. Like this -

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 

Your card variable is a nested list. To get a smoother looking output of lists (and other iterables), join each item with a separator, eg a single space:

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

output:

x x
x o
o x
o o

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM