小编coo*_*aws的帖子

如何将字符串的所有排列作为字符串列表(而不是元组列表)?

目标是创建一个单词中某些字母的所有可能组合的列表...这很好,除了它现在最终作为具有太多引号和逗号的元组列表.

import itertools

mainword = input(str("Enter a word: "))
n_word = int((len(mainword)))

outp = (list(itertools.permutations(mainword,n_word)))
Run Code Online (Sandbox Code Playgroud)

我想要的是:

[yes, yse, eys, esy, sye, sey]
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

[('y', 'e', 's'), ('y', 's', 'e'), ('e', 'y', 's'), ('e', 's', 'y'), ('s', 'y', 'e'), ('s', 'e', 'y')]
Run Code Online (Sandbox Code Playgroud)

在我看来,我只需要删除所有括号,引号和逗号.

我试过了:

def remove(old_list, val):
  new_list = []
  for items in old_list:
    if items!=val:
        new_list.append(items)
  return new_list
  print(new_list)
Run Code Online (Sandbox Code Playgroud)

我只是运行了几次这个功能.但它不起作用.

python tuples list python-itertools python-3.x

4
推荐指数
1
解决办法
149
查看次数

标签 统计

list ×1

python ×1

python-3.x ×1

python-itertools ×1

tuples ×1