这个漂亮的脚本生成给定集合s
的所有4个字符排列,并将它们打印在新行上。
import itertools
s = ('7', '8', '-')
l = itertools.product(s, repeat=4)
print(*l, sep='n')
样本输出:
...
('9', '-', '7', '8')
('9', '-', '7', '9')
('9', '-', '8', '7')
('9', '-', '8', '8')
('9', '-', '8', '9')
...
我不知道如何删除所有的单引号、逗号和左/右括号。
所需输出:
...
9-78
9-79
9-87
9-88
9-89
...
尝试添加:
c = []
for i in l:
i = i.replace(",", '')
c.append(i)
print(*c, sep='n')
错误:AttributeError: 'tuple' object has no attribute 'replace'
也尝试过:我似乎找不到print(' '.join())
逻辑的位置。
每次打印值时,都可以使用:
for vals in l:
print("".join([str(v) for v in vals]))
这只是连接所有字符,注意.join
要求值为字符串。
您也可以使用:
for vals in l:
print(*vals)
但这在值之间有一个空间。