如何找到字典值的位置 [英] how to find position of dictionary values

查看:232
本文介绍了如何找到字典值的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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.

推荐答案

lookfor =''dfsdf''

表示项目,值为kev.items():

如果寻找价值:

打印商品

打印value.index(lookfor)

休息#假设你只想要一个结果

你也可以跳过''if''验证,在这种情况下你需要捕获

ValueError异常以防当前没有这样的条目清单。


希望它有所帮助。


lee写道:
lookfor = ''dfsdf''
for item, value in kev.items():
if lookfor in value:
print item
print value.index(lookfor)
break # assuming you only want one result
You can also skip the ''if'' verification in which case you need to catch
ValueError exception in case there is no such entry in the current list.

Hope it helps.

lee wrote:

hi ,

i有一个字典如下:

kev:{''phno'':[''dgsd'',''gsdg'',''dfsdf'' ,''g''],''发送电子邮件'':[''dg'',

''sgsd'','sdfsdf'',''gdf''],'' name'':[''ds'',''dsg'',''dsfds'',''fgdf''],

''地址'':[''sdg'' ,''dsgsdg'',' SDF, DFG]}

$如果用户输入键PHNO的第三项B $ B,即" dfsdf"在我的词典中,

我怎么能找到它是

phno内部列表中的第三项呢?谢谢。

-
http://mail.python.org/mailman/listinfo/python-list


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.
--
http://mail.python.org/mailman/listinfo/python-list


9月1日,1 :21 pm,Alexandru Palade

< alexandru.pal ... @ sellerengine.comwrote:
On Sep 1, 1:21 pm, Alexandru Palade
<alexandru.pal...@sellerengine.comwrote:

lookfor =''dfsdf''

为物品,值为kev.items():

如果寻找值:

打印物品

打印value.index(lookfor)

休息#假设你只想要一个结果


你也可以跳过if验证,在这种情况下你需要捕获

ValueError异常,以防当前列表中没有此类条目。


希望它有所帮助。


lee写道:
lookfor = ''dfsdf''
for item, value in kev.items():
if lookfor in value:
print item
print value.index(lookfor)
break # assuming you only want one result

You can also skip the ''if'' verification in which case you need to catch
ValueError exception in case there is no such entry in the current list.

Hope it helps.

lee wrote:

hi,

i有一个字典如下:

kev:{''phno'':[''dgsd'',''gsdg'',''dfsdf'',''g''],''email'':[''dg'',

''sgsd'','sdfsdf'',''gdf''],''name'':[''ds'',''dsg'',''dsfds' ','''fgdf''],

''地址'':['sdg'',''dsgsdg'',''sdf'',''dfg'']}

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'']}


如果用户输入密钥phno的第3项,即dfsdf,在我的词典中,

我怎么能找到它是

phno内部列表中的第三项呢?谢谢。

-
http://mail.python.org/mailman/listinfo/python-list



谢谢你的解决方案正是我想要的: )

thank u your solution is exactly wat i wanted :)


leeaécrit:
lee a écrit :

hi,

i有一个字典作为如下:

kev:{''phno'':[''dgsd'',''gsdg'',''dfsdf'',''g''],''email'' :[''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?



这很简单(提示:阅读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....


这篇关于如何找到字典值的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆