下面的 python 代码给出了给定值的不同组合。
import itertools
iterables = [ [1,2,3,4], [88,99], ['a','b'] ]
for t in itertools.product(*iterables):
print t
输出:-
(1, 88, 'a')
(1, 88, 'b')
(1, 99, 'a')
(1, 99, 'b')
(2, 88, 'a')
等等。
谁能告诉我如何修改这段代码,使输出看起来像一个列表;
188a
188b
199a
199b
288a
最佳答案
您必须将数字转换为字符串,然后加入结果:
print ''.join(map(str, t))
如果您的输入字符串以以下开头,您就可以避免转换:
iterables = [['1', '2', '3', '4'], ['88', '99'], ['a', 'b']]
for t in itertools.product(*iterables):
print ''.join(t)
如果您想要的只是一起打印这些值(而不对它们做任何其他事情),那么使用print()
作为一个函数(通过使用from __future__ import print_function
Python 2 功能切换或使用 Python 3):
from __future__ import print_function
iterables = [[1, 2, 3, 4], [88, 99], ['a', 'b']]
for t in itertools.product(*iterables):
print(*t)
关于python - 将此 python 代码的输出更改为列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44355493/