how to find position of dictionary values

Discussion in 'Python' started by lee, Sep 1, 2008.

  1. lee

    lee Guest

    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.
     
    lee, Sep 1, 2008
    #1
    1. Advertising

  2. 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
    >
    >
     
    Alexandru Palade, Sep 1, 2008
    #2
    1. Advertising

  3. lee

    lee Guest

    On Sep 1, 1:21 pm, Alexandru Palade
    <> 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 :)
     
    lee, Sep 1, 2008
    #3
  4. 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....
     
    Bruno Desthuilliers, Sep 1, 2008
    #4
  5. lee

    lee Guest

    On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
    > 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?
     
    lee, Sep 1, 2008
    #5
  6. lee

    lee Guest

    On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
    > 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?
     
    lee, Sep 1, 2008
    #6
  7. lee

    lee Guest

    On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
    > 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?
     
    lee, Sep 1, 2008
    #7
  8. lee

    lee Guest

    On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
    > 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?
     
    lee, Sep 1, 2008
    #8
  9. 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.
     
    Bruno Desthuilliers, Sep 1, 2008
    #9
  10. lee wrote:

    > On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
    > > 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
     
    Diez B. Roggisch, Sep 1, 2008
    #10
  11. lee

    lee Guest

    On Sep 1, 2:37 pm, "Diez B. Roggisch" <> wrote:
    > lee wrote:
    > > On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
    > > > 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': ['', '1234', 'missuri'], 'shri':
    ['', '23423', 'india'], 'marsa': ['',
    '2345', 'brazil'], 'sandeep': ['', '007',
    'canada']}


    how can i change the key to something like 'sabir' and how can i
    change the values of kabir?
     
    lee, Sep 1, 2008
    #11
  12. lee

    lee Guest

    On Sep 1, 3:59 pm, Wojtek Walczak <> wrote:
    > On Mon, 1 Sep 2008 03:51:13 -0700 (PDT), lee wrote:
    > > 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': ['', '1234', 'missuri'], 'shri':
    > > ['', '23423', 'india'], 'marsa': ['',
    > > '2345', 'brazil'], 'sandeep': ['', '007',
    > > 'canada']}
    > > how can i change the key to something like 'sabir' and

    >
    > kev['sabir'] = kev['kabir']
    > del kev['kabir']
    >
    > > how can i
    > > change the values of kabir?

    >
    > kev['sabir'][0] = ''
    >
    > *untested*
    >
    > --
    > Regards,
    > Wojtek Walczak,http://tosh.pl/gminick/


    thanks wojtek, it worked :)
     
    lee, Sep 1, 2008
    #12
  13. lee a écrit :
    > On Sep 1, 2:37 pm, "Diez B. Roggisch" <> wrote:
    >> lee wrote:

    (snip)
    >>> 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?


    <ot>

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


    > i am soory for that keystrokes


    Is your cap key broken ?-)

    </ot>

    (snip)
    >. can anyone tell me how can i change
    > the value of key.
    > suppose i have a dictionary
    >
    > kev = {'kabir': ['', '1234', 'missuri'], 'shri':
    > ['', '23423', 'india'], 'marsa': ['',
    > '2345', 'brazil'], 'sandeep': ['', '007',
    > 'canada']}
    >
    >
    > how can i change the key to something like 'sabir' and how can i
    > change the values of kabir?


    I don't mean to be harsh, but did you read the FineManual(tm) ? All this
    is very basic (no pun) stuff, mostly documented, and easy to try out
    using the interactive interpreter. This ng is very newbie-friendly, but
    this doesn't mean you can hope to get by without doing the minimal
    effort of going thru the tutorial and trying a couple things by yourself
    before asking.

    Hope you'll understand the above remark intends to be an helpful advice...
     
    Bruno Desthuilliers, Sep 1, 2008
    #13
  14. lee

    Terry Reedy Guest

    Alexandru Palade 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


    slight variation:

    lookfor = 'dfsdf'
    for item, value in kev.items():
    for i, val in enumerate(value):
    if val == lookfor:
    print item, i
    break # assuming you only want one result
    else:
    print lookfor, 'not found'

    This is what for-else is meant for.

    If you want 0 to many lookfor occurences,

    lookfor = 'dfsdf'
    hits = []
    for item, value in kev.items():
    for i, val in enumerate(value):
    if val == lookfor:
    hits.append((item, i))

    print hits

    One-liner fanatics would, of course, rewrite this as

    hits = [(item, i) for item, value in kev.items() for i, val in
    enumerate(value) if val == lookfor]

    Terry Jan Reedy



    tjr
     
    Terry Reedy, Sep 1, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    463
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    347
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,259
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    558
    Chris Rebert
    Jul 29, 2010
  5. macm
    Replies:
    8
    Views:
    345
    Alexander Gattin
    Nov 6, 2010
Loading...

Share This Page