gpt4 book ai didi

python - 在python中查找字典

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:21 25 4
gpt4 key购买 nike

所以我有这样的多行文件(空格分隔 rune 件):

A1BG      P04217     VAR_018369  p.His52Arg     Polymorphism  rs893184    -
A1BG P04217 VAR_018370 p.His395Arg Polymorphism rs2241788 -
AAAS Q9NRG9 VAR_012804 p.Gln15Lys Disease - Achalasia

如何制作字典以在第二列中查找 id 并将数字(单词之间)存储在第四列中。

我试过了,但它给了我超出范围的索引

lookup = defaultdict(list)
with open ('humsavar.txt', 'r') as humsavarTxt:
for line in csv.reader(humsavarTxt):
code = re.match('[a-z](\d+)[a-z]', line[1], re.I)
if code:
lookup[line[-2]].append(code.group(1))

print lookup['P04217']

最佳答案

这是原始代码的变体:

import csv, re
from collections import defaultdict

lookup = defaultdict(list)
with open('humsavar.txt', 'rb') as humsavarTxt:
reader = csv.reader(humsavarTxt, delimiter=" ", skipinitialspace=True)
for line in reader:
code = re.search(r'(\d+)', line[3])
lookup[line[1]].append(int(code.group(1)))

产生

>>> lookup
defaultdict(<type 'list'>, {'P04217': [52, 395], 'Q9NRG9': [15]})
>>> lookup['P04217']
[52, 395]

关于python - 在python中查找字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12099503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com