473,738 Members | 1,949 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
14 3814
lee
On Sep 1, 2:37 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
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 yielda
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
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': ['k****@kabir.co m', '1234', 'missuri'], 'shri':
['s***@shri.com' , '23423', 'india'], 'marsa': ['m****@marsa.co m',
'2345', 'brazil'], 'sandeep': ['s******@sandee p.com', '007',
'canada']}
how can i change the key to something like 'sabir' and how can i
change the values of kabir?
Sep 1 '08 #11
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': ['k****@kabir.co m', '1234', 'missuri'], 'shri':
['s***@shri.com' , '23423', 'india'], 'marsa': ['m****@marsa.co m',
'2345', 'brazil'], 'sandeep': ['s******@sandee p.com', '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] = 's****@sabir.co m'

*untested*

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 1 '08 #12
lee
On Sep 1, 3:59 pm, Wojtek Walczak <gmin...@bzt.bz twrote:
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': ['ka...@kabir.co m', '1234', 'missuri'], 'shri':
['s...@shri.com' , '23423', 'india'], 'marsa': ['ma...@marsa.co m',
'2345', 'brazil'], 'sandeep': ['sand...@sandee p.com', '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] = 'sa...@sabir.co m'

*untested*

--
Regards,
Wojtek Walczak,http://tosh.pl/gminick/
thanks wojtek, it worked :)
Sep 1 '08 #13
lee a écrit :
On Sep 1, 2:37 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
>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': ['k****@kabir.co m', '1234', 'missuri'], 'shri':
['s***@shri.com' , '23423', 'india'], 'marsa': ['m****@marsa.co m',
'2345', 'brazil'], 'sandeep': ['s******@sandee p.com', '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...
Sep 1 '08 #14


Alexandru Palade wrote:
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
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((it em, 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

Sep 1 '08 #15

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

Similar topics

1
1543
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
8731
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
6079
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
3191
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
1964
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
2035
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
1841
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
1420
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
2370
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
8968
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
8787
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
9473
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...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
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...
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.