首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python删除逗号而不使用replace

Python删除逗号而不使用replace
EN

Stack Overflow用户
提问于 2017-10-24 13:00:06
回答 2查看 108关注 0票数 0

我有一个python输出,它是输入到tKinter图形用户界面中的所有单词的排列。我需要把通讯从输出中去掉。我使用了replace,但每次置换后都会出现一个空格。

代码语言:javascript
复制
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"

正如您在上面看到的,每个排列输出都有一个逗号,我已经将其替换为空格。我的目标是不使用空格,而是去掉逗号,以便所有输出都与输出的第一行对齐。我该怎么做呢?任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2017-10-24 13:01:27

对结果进行迭代。

代码语言:javascript
复制
for perm in perms:
  print(perm)
票数 2
EN

Stack Overflow用户

发布于 2017-10-24 13:42:53

另一种方式:

代码语言:javascript
复制
for perm in perms:
    print(*perm)

其中perms的类型为<class 'itertools.permutations'>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46902533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档