python - 将此 python 代码的输出更改为列表?

标签 python

下面的 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/

相关文章:

python - 如何在我的 Web 服务器上运行 Python 脚本?

python - 填充 numpy 数组的对角线失败

python - 多级 argparse 子解析器

python - 具有多个条件的数组索引操作

Python循环速度源

c++ - Python 列表和 c/c++ 数组

python - Matplotlib 'key_press_event' 没有响应

python - 将完整的 virtualenv 复制到另一台电脑

python - scipy.stats.chisquare 没有给出输入数据预期的结果

python - 在 python 模块中导入子模块时 PyImport_Import 失败