Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to find position of dictionary values

Reply
Thread Tools

how to find position of dictionary values

 
 
lee
Guest
Posts: n/a
 
      09-01-2008
hi,
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.
 
Reply With Quote
 
 
 
 
Alexandru Palade
Guest
Posts: n/a
 
      09-01-2008
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 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
>
>


 
Reply With Quote
 
 
 
 
lee
Guest
Posts: n/a
 
      09-01-2008
On Sep 1, 1:21 pm, Alexandru Palade
<(E-Mail Removed)> wrote:
> 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 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


hi, thank u your solution is exactly wat i wanted
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-01-2008
lee a écrit :
> hi,
> 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?


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....
 
Reply With Quote
 
lee
Guest
Posts: n/a
 
      09-01-2008
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
(E-Mail Removed)> wrote:
> lee a écrit :
>
> > hi,
> > 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?

>
> 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....


hi,
i agree with u, my data strusture is not efficient. but all the
records,viz...name,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
 
Reply With Quote
 
lee
Guest
Posts: n/a
 
      09-01-2008
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
(E-Mail Removed)> wrote:
> lee a écrit :
>
> > hi,
> > 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?

>
> 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....


hi,
i agree with u, my data strusture is not efficient. but all the
records,viz...name,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
 
Reply With Quote
 
lee
Guest
Posts: n/a
 
      09-01-2008
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
(E-Mail Removed)> wrote:
> lee a écrit :
>
> > hi,
> > 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?

>
> 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....


hi,
i agree with u, my data strusture is not efficient. but all the
records,viz...name,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
 
Reply With Quote
 
lee
Guest
Posts: n/a
 
      09-01-2008
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
(E-Mail Removed)> wrote:
> lee a écrit :
>
> > hi,
> > 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?

>
> 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....


hi,
i agree with u, my data strusture is not efficient. but all the
records,viz...name,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-01-2008
lee a écrit :
>
> hi, thank u your solution is exactly wat i wanted


I'm afraid it's not what you actually *need*, cf my other post.
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      09-01-2008
lee wrote:

> On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
> (E-Mail Removed)> wrote:
>> lee a écrit :
>>
>> > hi,
>> > 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?

>>
>> 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....

>
> hi,
> i agree with u, my data strusture is not efficient. but all the
> records,viz...name,phno, email,address are all generated at runtime ,
> when the user enters them. so how can i design my datastructure in
> that case?


Are "u" short on keystrokes? You are not textmessaging here...

Regarding the actual question: there is no difference in building your or
the other structure. It's only a question of which key you use first.
Instead of first looking up the type of the record ("phno" or some such),
do that with the name of the user. If no record exists, create one. Then
populate the record with the user's values. Like this:

user = "dsdf"
phonenumber = "123"

record = records.setdefault(user, {})
record["phno"] = phonenumber

Diez
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How find all childrens values of a nested dictionary, fast! macm Python 8 11-06-2010 05:51 PM
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM



Advertisments