简体   繁体   English

python:元组列表中的元组链元素

[英]python: chain elements of a tuple in a list of tuples

I want to find combinations of elements in a string. 我想找到字符串中元素的组合。 baseString is of variable length eg 'tbyhn' or 'tg' or so on. baseString的长度可变,例如'tbyhn'或'tg'等。

I tried: 我试过了:

import itertools

baseString = 'tgby'
prd = [it for it in itertools.product(base,repeat=len(baseString)-1)]

prd is a list that looks like this: prd是一个看起来像这样的列表:

[('t', 't', 't'), ('t', 't', 'g'), ('t', 't', 'b'), ..., ('y', 'y', 'y')]

I would like the list to look like this: 我希望列表看起来像这样:

['ttt','ttg','ttb','tty','tgt',...,'yyy']

How can I do that? 我怎样才能做到这一点?

Also, if I ever have a list of tuples like 'prd' how do I chain only the elements that are in each tuple. 另外,如果我有像“ prd”这样的元组列表,我如何只链接每个元组中的元素。

EDIT 编辑

I didn't want these types of outcomes: 我不想要这些类型的结果:

   x = ['t','t','t','t','t','g','t','t','b','t','t','y',...,'y','y','y']

   x = ['tttttgttbttytgt...yyy']

Simply join them like this 像这样简单地加入他们

bS = 'tgby'
prd = ["".join(it) for it in itertools.product(bS, repeat=len(bS)-1)]

Edit: A faster version, suggested by @alko in the comments 编辑:一个更快的版本,由@alko在评论中建议

prd = map(''.join, itertools.product(bS, repeat=len(bS)-1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM