簡體   English   中英

python:元組列表中的元組鏈元素

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

我想找到字符串中元素的組合。 baseString的長度可變,例如'tbyhn'或'tg'等。

我試過了:

import itertools

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

prd是一個看起來像這樣的列表:

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

我希望列表看起來像這樣:

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

我怎樣才能做到這一點?

另外,如果我有像“ prd”這樣的元組列表,我如何只鏈接每個元組中的元素。

編輯

我不想要這些類型的結果:

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

   x = ['tttttgttbttytgt...yyy']

像這樣簡單地加入他們

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

編輯:一個更快的版本,由@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