[英]Python: List of tuples: compare all tuples and retrive tuples where the elements of tuples are not equal to any other tuple
[英]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.