我已经从文件中创建了一个嵌套字典:
import csv
d = {}
with open("file.txt", 'r') as f:
data = csv.DictReader(f, delimiter="\t")
for row in data:
item = d.get(row["value"], dict())
item[row["value"]] = row["need"]
d[row["item"]] = item
print d
但是,每个项目应该有多个条目。例如,以下是此代码的输出结果:
d = {'key1': {'153908482': 'ctaggaacca'}, 'key2': {'115057116': 'gtaattctga'}, 'key3': {'133381371': 'ctgaaaagat'}
字典看起来应该是这样的:
d = {'key1': {'153908482': 'ctaggaacca', '143543': 'atccgatcgg'}, 'key2': {'115057116': 'gtaattctga', '12321333': 'accccgta'}, 'key3': {'133381371': 'ctgaaaagat'}
我在创建字典时丢失了一些信息。任何帮助将不胜感激。
答案 0 :(得分:1)
问题在于您将item
分配给与从词典获得的密钥不同的密钥。
item = d.get(row["value"], dict()) <-- you never assign to row["value"] so
this always returns an empty dict
item[row["value"]] = row["need"]
d[row["item"]] = item <-- since you didn't get the existing
row["item"], you keep overwriting
them
我稍微重写了你的脚本,以便它可以使用测试数据运行,但唯一真正的改变是从正确的密钥中获取数据
import csv
from cStringIO import StringIO
f = StringIO("""item value need
key1 153908482 ctaggaacca
key2 115057116 gtaattctga
key3 133381371 ctgaaaagat
key1 143543 atccgatcgg
key2 212321333 accccgta
""")
d = {}
#with open("file.txt", 'r') as f:
data = csv.DictReader(f, delimiter="\t")
for row in data:
# item = d.get(row["value"], dict())
item = d.get(row["item"], dict())
item[row["value"]] = row["need"]
d[row["item"]] = item
print d
这是使用defaultdict
的替代实现,它为您创建内部字典
import collections
d = collections.defaultdict(dict)
data = csv.DictReader(f, delimiter="\t")
for row in data:
d[row["item"]][row["value"]] = row["need"]
print d