【发布时间】:2016-09-13 05:17:36
【问题描述】:
我有一个 txt 文件,我想将值读入字典。与普通字典不同,每个key的值是一个值对,例如:
tiger eat meat
tiger eat people
rabbit eat carrot
people can walk
trees has root
people has hand
我想要一本字典,
tiger, {eat, meat}, {eat, people}
rabbit, {eat, carrot}
trees, {has, root}
people, {can, walk}, {has, hand}
我应该只将read lines、split(\n) 分成 3 个项目并将第一个作为键存储,其余两个作为值存储吗?还是有更好的方法来存储这两个值?
我的目标是,当我查询老虎吃什么时,我想得到答案meat 和people。
【问题讨论】:
-
你能显示你想要的实际字典结构,而不是这个奇怪的非 Python 伪代码吗?
-
你需要上传你到目前为止所做的事情。更重要的是,您的数据结构没有任何字典。这是你想要的:
{'tiger': [{'eat': 'meat'}, {'eat': 'people'}]}?
标签: python dictionary