python - checking if a list is a key in a dictionary and returning corresponding value
问题描述
This is my code:
with open('file.txt','r') as f:
table = {}
for lines in f:
co1,co2,co3,pro1,pro2,pro3 = (
item.strip() for item in lines.split(',',5))
codon=str(co1 + co2 + co3)
table[codon] = pro2
print(table)
I need to split the dan_seq
whatever they say it is into chunks of 3 and then match it to the dictionary I created
i.e. if the chunk is a key in my dictionary, return the value of it
dna_seq = list('AAAGTTAAATAATAAATAGGTGAA')
the picture is the text file:
解决方案
您可以使用切片来分块dna_seq
,然后table.get
获取匹配值:
>>> dna_seq = 'AAAGTTAAATAATAAATAGGTGAA'
>>> [dna_seq[i:i+3] for i in range(0, len(dna_seq), 3)]
['AAA', 'GTT', 'AAA', 'TAA', 'TAA', 'ATA', 'GGT', 'GAA']
>>> for chunk in [dna_seq[i:i+3] for i in range(0, len(dna_seq), 3)]:
... print(table.get(chunk))
推荐阅读
- android - 错误:android.view.WindowManager$BadTokenException
- augmented-reality - SensorStreamViewer,HoloLensForCV:“初始化媒体捕获失败:访问被拒绝。”
- javascript - 无法在 JavaScript 中使用 onload() 访问表单元素
- linq - LINQ 分组和排序无法按预期工作
- swift - 尝试访问 healthkit 时出现 containerAppExtensionEntitlementsWithCompletion 错误
- javascript - Typescript + select2 + browserify = TypeError: $(...).select2 不是函数
- vba - 如何在数据透视表合并中使用 R1C1 范围内的变量,excel 2010?
- haskell - 我可以根据某个类的可用性过滤类型级别列表吗?
- macos - MacOS 上的 Jenkins 从属 Java 路径
- android - 使用 Kotlin 的 RTL 布局菜单项的方向