473,698 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to find position of dictionary values

lee
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.
Sep 1 '08 #1
14 3810
lookfor = 'dfsdf'
for item, value in kev.items():
if lookfor in value:
print item
print value.index(loo kfor)
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

Sep 1 '08 #2
lee
On Sep 1, 1:21 pm, Alexandru Palade
<alexandru.pal. ..@sellerengine .comwrote:
lookfor = 'dfsdf'
for item, value in kev.items():
if lookfor in value:
print item
print value.index(loo kfor)
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 :)
Sep 1 '08 #3
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....
Sep 1 '08 #4
lee
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
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...n ame,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
Sep 1 '08 #5
lee
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
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...n ame,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
Sep 1 '08 #6
lee
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
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...n ame,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
Sep 1 '08 #7
lee
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
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...n ame,phno, email,address are all generated at runtime ,
when the user enters them. so how can i design my datastructure in
that case?
Sep 1 '08 #8
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.
Sep 1 '08 #9
lee wrote:
On Sep 1, 1:45 pm, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
>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...n ame,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.setdefa ult(user, {})
record["phno"] = phonenumber

Diez
Sep 1 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1541
by: christine.nguyen | last post by:
I'm using offset values to find the x,y position of an iframe on an html page. The code works in all browsers except in Mac explorer 5.x. Does anybody have any ideas how to get around this?? Thanks. My code is below, assuming myFrame is the iframe in question, offsetLeft is the x coordinate, offsetTop is the y coordinate. For some reason, offsetParent for the iframe on Mac Explorer is always undefined. Even stranger, this is only the...
2
8728
by: rbt | last post by:
What's a good way to compare values in dictionaries? I want to find values that have changed. I look for new keys by doing this: new = if new == : print new, "No new files." else: print new, "New file(s)!!!" My key-values pairs are filepaths and their modify times. I want to
11
6076
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for dicts with larger no. of features. Now i want to check each pair to see if they are connected...element of this pair will be one from the first list and one from the second....e.g for 'ab' i want to check if 1 and 3 are connected,then 1 and...
9
3187
by: jojoba | last post by:
hello! i am trying to come up with a simple way to access my values in my nested python dictionaries here is what i have so far, but i wanted to run it by the geniuses out there who might see any probems with this... here is an example: +++++++++++++++++++++++++++++++++++++++
5
1962
by: j1o1h1n | last post by:
Hello, I was trying to create a flattened list of dictionary values where each value is a list, and I was hoping to do this in some neat functionally style, in some brief, throwaway line so that it would assume the insignificance that it deserves in the grand scheme of my program. I had in mind something like this:
1
2034
by: Steve Richter | last post by:
the win32 API documentation references a lot of symbolic values. FILE_SHARE_READ, GENERIC_WRITE, ... http://msdn2.microsoft.com/en-us/library/ms959950.aspx are the actual values enumerated at the MSDN site? I know I can find them in the C++ header files, just would like to have a definitive place on MSDN to find all the values. -Steve
6
1839
by: kdt | last post by:
Can anyone suggest a better way of returning the values in a dictionary as a single list. I have the following, but it uses a nested loop, not sure if there is a more efficient way. >>> d= >>> d.append((56,4)) >>> d >>> s = >>> for i, j in enumerate(d): ... for k, l in enumerate(j):
0
1418
by: mp | last post by:
I am doing some volunteering for a local non-profit aid organization and not very experienced with access. I have a table which is a list of Federal Poverty Line cutoffs based on the number of people in a household. fpl_id NumPeople 75% FPL 100% FPL 125% FPL 150% FPL 1 1 $638.00 $851.00 $1,064.00 $1,276.00 2 2 $856.00 $1,141.00 $1,426.00 $1,711.00 3 3 $1,073.00 $1,431.00 $1,789.00 $2,146.00
1
2369
by: anwar7517525 | last post by:
Dearest how can i find three smallest values in array I solve it by sorting array in ASC Order then i print first three values but i want to another efficient way can you have any efficient way
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.