Reputation: 253
I have a list of tuples generated by this code.
ast = [3,1]
fgh = list(itertools.product(ast, repeat=3))
And I was wanting to turn it into a list of strings like this
['3, 3, 3', '3, 3, 1', '3, 1, 3', '3, 1, 1', '1, 3, 3', '1, 3, 1', '1, 1, 3', '1, 1, 1']
I have tried everything I could find, but I'm sure I'm missing something.
Keep getting this. TypeError: sequence item 0: expected string, int found. when i try to use .join().
this code is the closest I've gotten lol.
[('{} '*len(t)).format(*t).strip() for t in fgh]
and I get this
['3 3 3', '3 3 1', '3 1 3', '3 1 1', '1 3 3', '1 3 1', '1 1 3', '1 1 1']
Thanks, I really appreciate it.
Upvotes: 0
Views: 48
Reputation: 473833
You can use str.join()
instead using ,
as a delimiter and also converting individual items to string (this is what the map(str, item)
is responsible for) for the str.join()
to work:
>>> import itertools
>>> ast = [3,1]
>>> fgh = itertools.product(ast, repeat=3)
>>> [", ".join(map(str, item)) for item in fgh]
['3, 3, 3', '3, 3, 1', '3, 1, 3', '3, 1, 1', '1, 3, 3', '1, 3, 1', '1, 1, 3', '1, 1, 1']
Upvotes: 2