coo*_*aws 4 python tuples list python-itertools python-3.x
目标是创建一个单词中某些字母的所有可能组合的列表...这很好,除了它现在最终作为具有太多引号和逗号的元组列表.
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)
我只是运行了几次这个功能.但它不起作用.
你可以用这样的理解重新组合这些元组:
new_list = [''.join(d) for d in old_list]
Run Code Online (Sandbox Code Playgroud)
data = [
('y', 'e', 's'), ('y', 's', 'e'), ('e', 'y', 's'),
('e', 's', 'y'), ('s', 'y', 'e'), ('s', 'e', 'y')
]
data_new = [''.join(d) for d in data]
print(data_new)
Run Code Online (Sandbox Code Playgroud)
['yes', 'yse', 'eys', 'esy', 'sye', 'sey']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |