1
我有一個包含此信息的文件:我想創建一個字典,其中NM_
的是鍵和開始和結束都是通過定義一個函數創建的文件字典
#chrom start end isoform
chr1 75 90 NM_100
chr1 100 120 NM_100
chr2 25 50 NM_200
chr2 55 75 NM_200
chr2 100 125 NM_200
chr2 155 200 NM_200
從這個文件價值。像這樣:
dictionary = {NM_100: [(75, 90), (100,120)], NM_200: [(25, 50), (55,75), (100, 125), (155, 200)]}
我一直在嘗試使用此代碼生成功能,讓我拉上的開始和結束,但我似乎無法得到它的正常工作。
def read_exons(line):
parts = iter(line.split())
chrom = next(parts)
start = next(parts)
end = next(parts)
isoform = next(parts)
return isoform, [(s, e) for s, e in zip(start, end)]
with open('test_coding.txt') as f:
exons = dict(read_exons(line) for line in f
if not line.strip().startswith('#'))
據我所知,功能不會讓我追加到值,但我在努力弄清楚如何連得開始和結束一行在字典中正確顯示。有任何想法嗎? iter()
或zip
有問題嗎?
真棒!我試圖使用這種技術,但它給了我一個錯誤:'ValueError:對於int()以10爲底的無效文字:'開始' – interstellar
從頭開始。我得到它的工作!謝謝!這麼簡單:) – interstellar