how to find position of dictionary values

lee san82moon at gmail.com
Mon Sep 1 06:51:13 EDT 2008


On Sep 1, 2:37 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> lee wrote:
> > On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
> > 42.desthuilli... at websiteburo.invalid> 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

i am soory for that keystrokes. can anyone tell me how can i change
the value of key.
suppose i have a dictionary

kev =  {'kabir': ['kabir at kabir.com', '1234', 'missuri'], 'shri':
['shri at shri.com', '23423', 'india'], 'marsa': ['marsa at marsa.com',
'2345', 'brazil'], 'sandeep': ['sandeep at sandeep.com', '007',
'canada']}


how can i change the key to something like 'sabir' and how can i
change the values of kabir?



More information about the Python-list mailing list