简体   繁体   English

如何从具有列表值的字典中获取一系列键来检索所有可能的组合

[英]How to retrieve all possible combinations given a sequence of keys from a dictionary with list values

I have for instance this dictionary 我有这个字典

d={'M':['ATG'],'D':['GAC','GAT'],'E':['GAA','GAG']}

What I'd like to have as an output given a sequence of keys is a list with all possible sequences. 在给定一系列键的情况下,我想要的输出是包含所有可能序列的列表。 (could be a string as well, in which all the possible sequences would be in separate lines "\\n") (也可以是一个字符串,其中所有可能的序列都在单独的行中“\\ n”)

sequence = "MDE" 

So, the output should be the following: 所以,输出应该如下:

['ATGGACGAA','ATGGACGAG','ATGGATGAA','ATGGATGAG']

What I've tried so far is the following, but of course it's not what I want: 到目前为止我所尝试的是以下内容,但当然这不是我想要的:

seq_trans = ''

for aa in sequence:
  for k, v in d.iteritems():
    if k == aa:
      for item in v:
        seq_trans= seq_trans + item
print seq_trans

And what I get for "MDE" is: 我得到的“MDE”是:

'ATGGACGATGAAGAG'

You can use itertools.product here, it returns Cartesian product of the input iterables. 你可以在这里使用itertools.product ,它返回输入迭代的笛卡尔积。

In [78]: seq="MED"

In [79]: ["".join(x) for x in product(*(d[y] for y in seq))]
Out[79]: ['ATGGAAGAC', 'ATGGAAGAT', 'ATGGAGGAC', 'ATGGAGGAT']

暂无
暂无

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

相关问题 如何从python中的字典中检索具有给定值的所有键 - how to retrieve all keys with a given value from dictionary in python 如何用所有可能的组合替换字典中列表中的字符串 - How to replace strings in list from a dictionary with all possible combinations 从字典和列表中获取所有可能的组合 - Get all possible combinations from dictionary and list 给定一个以字符串列表作为其值的字典,您将如何检索列表包含所有其他列表唯一的字符串的所有键? - Given a dictionary with lists of strings as their values, how would you retrieve all keys where the list contains a string unique to all other lists? 如何从给定的字典中获取所有组合? - How to get all combinations from a given dictionary? 给定输入字符串的字典值的所有可能组合。 蟒蛇 - All possible combinations of dictionary values given input string. Python 从给定序列计算所有可能的组合(在python中) - Calculate all the possible combinations from a given sequence (in python) 在此字典中查找最多八 (8) 个键的所有可能组合,前提是这些键的值加起来等于 45 - Find all possible combinations of maximum eight (8) keys in this dictionary, only if the values of those keys add up to 45 如果键的所有值都是 null,如何从列表中删除字典 - How to remove dictionary from a list if all values of keys are null 来自给定类别的值的所有可能组合的数据帧 - Dataframe from all possible combinations of values of given categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM