我有一个python输出,它是输入到tKinter图形用户界面中的所有单词的排列。我需要把通讯从输出中去掉。我使用了replace,但每次置换后都会出现一个空格。
def exactMatch(entries):
words = [entry[1].get() for entry in entries]
perms = [p for p in permutations((words))]
x1 = str(perms)
perms2 = x1.replace("," , '')
perms3 = perms2.replace("'" , '') #Takes out the Quotations
perms4 = perms3.replace("(" , '[')
perms5 = perms4.replace(')' , ']\n')
perms6 = perms5.replace (" ", "")
print(perms6)
"a b c hello"
"a b hello c"
"a c b hello"
"a c hello b"
"a hello b c"
"a hello c b"
"b a c hello"
"b a hello c"
"b c a hello"
"b c hello a"
"b hello a c"正如您在上面看到的,每个排列输出都有一个逗号,我已经将其替换为空格。我的目标是不使用空格,而是去掉逗号,以便所有输出都与输出的第一行对齐。我该怎么做呢?任何帮助都是非常感谢的。
发布于 2017-10-24 13:01:27
对结果进行迭代。
for perm in perms:
print(perm)发布于 2017-10-24 13:42:53
另一种方式:
for perm in perms:
print(*perm)其中perms的类型为<class 'itertools.permutations'>
https://stackoverflow.com/questions/46902533
复制相似问题