简体   繁体   中英

Formatting print from itertools product

I am trying to print a list of every combination of a certain set of characters, I am using Product from itertools to do that. I have code that works for the printing of every set of characters, but, the formatting is not what i want.

Example of working code with wrong format:

from itertools import product

comb = product([1, "a", "."], repeat=3)

for i in comb:
    print(i)

The output of the code has the characters ( ' , ) which I don't want. If someone can help me get an output of what the combonation would be by its self without these characters I would really appreciate that.

You can think of comb as being a list of all combinations as tuples.

So just unpack each item while iterating, then you can also handle seperatly:

for (v0, v1, v2) in comb:
    print(v0, v1, v2)

Result:

1 1 1
1 1 a
1 1 .
1 a 1
...

print(*i) should do the trick.

Explanation : the * unpack items from an iterable, so it is equivalent to : print(i[0],i[1],i[2])

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