问题描述
i有一个字典如下:
kev:{''phno'':[''dgsd'',''gsdg'',' 'dfsdf'',''g''],''发送电子邮件'':[''dg'',
''sgsd'','sdfsdf'',''gdf'' ],''name'':[''ds'',''dsg'',''dsfds'',''fgdf''],
''地址'':[' 'sdg'',''dsgsdg'',''sdf'',''dfg'']}
如果用户输入密钥phno的第3项,即 dfsdf"在我的词典中,
我怎么能找到它是
phno内部列表中的第三项呢?谢谢。
i have a dictionary as follows :
kev : {''phno'': [''dgsd'', ''gsdg'', ''dfsdf'', ''g''], ''email'': [''dg'',
''sgsd'', ''sdfsdf'', ''gdf''], ''name'': [''ds'', ''dsg'', ''dsfds'', ''fgdf''],
''address'': [''sdg'', ''dsgsdg'', ''sdf'', ''dfg'']}
if user is enters the 3rd item of key phno, ie "dfsdf" in my dict,
how can i find it is the third item in the internal list of phno of
that dictionary? thanks you.
推荐答案
谢谢你的解决方案正是我想要的: )
thank u your solution is exactly wat i wanted :)
这很简单(提示:阅读FineManual(tm)获取dict.items()和
list.index( )),但是1 /完全低效且2 /不保证产生单个价值(如果''dfsdf'恰好也是
$ b $的第4项) b列表绑定到键''地址''?)。
我建议您重新考虑数据结构吗?你有什么
这里显然是''phno / email / name / address''records的集合。
这些记录不应该分成不同的对象。假设
''phno''是每个记录的唯一标识符,更好的数据结构
将是:
记录= {
''dgsd'':{''email'':''dg'',''name'':''ds'',''address'':'sdg ''},
''gsdg'':{''email'':''sgsd'',''name'':''ds'',''address'':' 'dsgsdg''},
#etc
}
这样,查找尽可能简单有效。
我的2美分....
It''s quite simple (hint : read the FineManual(tm) for dict.items() and
list.index()), but 1/totally inefficient and 2/not garanteed to yield a
single value (what if ''dfsdf'' happens to be also the 4th item of the
list bound to key ''address'' ?).
May I suggest you rethink your data structure instead ? What you have
here is obviously a collection of ''phno/email/name/address''records.
These records shouldn''t be split across different objects. Assuming
''phno'' is a unique identifier for each record, a better data structure
would be:
records = {
''dgsd'' : {''email'': ''dg'', ''name'' : ''ds'', ''address'' : ''sdg''},
''gsdg'' : {''email'': ''sgsd'', ''name'':''ds'', ''address'' : ''dsgsdg''},
# etc
}
This way, the lookup is as simple and efficient as possible.
My 2 cents....
这篇关于如何找到字典值的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!