我有一個txt文件,我想讀取字典中的值。從普通字典不同的是,每個key
的值是值對,例如:如何從文件創建Python中的字典,其中的值是一個列表
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'}]}'? –
@MosesKoledoye是〜 – flyingmouse