473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting values from keys and Viceversa

440 Contributor
Hi,

Dictionary works in getting values from the key values.But I would like to get keys from values.

For example :

d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

d.get(102)

o/p: [0,1,0]

I need

i/p

[0,1,0]

o/p : 102

Thanks

PSB
Mar 17 '07 #1
10 1876
bartonc
6,596 Recognized Expert Expert
Hi,

Dictionary works in getting values from the key values.But I would like to get keys from values.

For example :

d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

d.get(102)

o/p: [0,1,0]

I need

i/p

[0,1,0]

o/p : 102

Thanks

PSB
Keeping an inverse dictionary is not uncommon:
Expand|Select|Wrap|Line Numbers
  1.  
  2. >>> d = {'a':1, 'b':2, 'c':3}
  3. >>> di = dict((v, k) for k, v in d.items())
  4. >>> di
  5. {1: 'a', 2: 'b', 3: 'c'}
  6. >>> 
Mar 17 '07 #2
psbasha
440 Contributor
Thanks for the reply.

How to handle the dictionary when we have a Key as a number and Values are list?

As I mentioned in my example

-PSB
Mar 18 '07 #3
psbasha
440 Contributor
Expand|Select|Wrap|Line Numbers
  1. Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> di = dict((v, k) for k, v in d.items())
  3.  
  4. Traceback (most recent call last):
  5.   File "<pyshell#9>", line 1, in -toplevel-
  6.     di = dict((v, k) for k, v in d.items())
  7. TypeError: list objects are unhashable
  8. >>> 
I am getting the above error when I applied for the example I mentioned

-PSB
Mar 18 '07 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> di = dict((v, k) for k, v in d.items())
  3.  
  4. Traceback (most recent call last):
  5.   File "<pyshell#9>", line 1, in -toplevel-
  6.     di = dict((v, k) for k, v in d.items())
  7. TypeError: list objects are unhashable
  8. >>> 
I am getting the above error when I applied for the example I mentioned

-PSB
Try this:
Expand|Select|Wrap|Line Numbers
  1. >>> dict(zip([str(x) for x in d.values()], d.keys()))
  2. {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
  3. >>> 
Lists are not hashable but strings are.
Mar 18 '07 #5
ghostdog74
511 Recognized Expert Contributor
Hi,

Dictionary works in getting values from the key values.But I would like to get keys from values.

For example :

d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

d.get(102)

o/p: [0,1,0]

I need

i/p

[0,1,0]

o/p : 102

Thanks

PSB
usually, the way to get values is through the values() method.
for this case, you can use the dictionary values() method.
eg
Expand|Select|Wrap|Line Numbers
  1. if  [0,1,0] in d.values():
  2.     print "found "
  3.  
Mar 18 '07 #6
psbasha
440 Contributor
Try this:
Expand|Select|Wrap|Line Numbers
  1. >>> dict(zip([str(x) for x in d.values()], d.keys()))
  2. {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
  3. >>> 
Lists are not hashable but strings are.
So we have to create the list as string and use it for inversing and use it.

Thanks in advance
PSB
Mar 18 '07 #7
bartonc
6,596 Recognized Expert Expert
So we have to create the list as string and use it for inversing and use it.

Thanks in advance
PSB
Not if you use tuples instead of lists.
Lists let you assign into them (they are mutable) as in
Expand|Select|Wrap|Line Numbers
  1. aList[index] = valuse
Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
Expand|Select|Wrap|Line Numbers
  1. pt = x, y, z
you can use tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
  2. >>> di = dict((v, k) for k, v in d.items())
  3. >>> di
  4. {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
  5. >>> 
Mar 19 '07 #8
bvdet
2,851 Recognized Expert Moderator Specialist
Not if you use tuples instead of lists.
Lists let you assign into them (they are mutable) as in
Expand|Select|Wrap|Line Numbers
  1. aList[index] = valuse
Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
Expand|Select|Wrap|Line Numbers
  1. pt = x, y, z
you can use tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
  2. >>> di = dict((v, k) for k, v in d.items())
  3. >>> di
  4. {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
  5. >>> 
Good work Barton. I didn't think of tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> dict(zip([tuple(x) for x in d.values()], d.keys()))
  3. {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
  4. >>> 
Mar 19 '07 #9
bartonc
6,596 Recognized Expert Expert
Good work Barton. I didn't think of tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> dict(zip([tuple(x) for x in d.values()], d.keys()))
  3. {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
  4. >>> 
Take values() and keys() from a dictionary separately scares me a little. But I suppose that there's no reason for the dict to be re-orderd until a new item is add or an old one removed...
Mar 19 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
18195
by: Mike | last post by:
Hi, Does anyone know of reliable programs that convert C# to Java and viceversa? Thanks Mike
8
59519
by: SenthilVel | last post by:
how to get the corresponding values for a given Key in hashtable ??
7
7906
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using the keys from the old dictionaries? The keys in the new dictionary would be the keys from the old dictionary one (dict1) and the values in the new dictionary would be the keys from the old dictionary two (dict2). The keys would be joined by...
41
3165
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB) SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15 --get entries just added
6
1463
by: Paul Rubin | last post by:
I'm thinking of proposing that a values method be added to set objects, analogously with dicts. If x is a set, x.values() would be the same as list(x). This feels logical, and it would allow unified treatment of dicts and sets in some contexts. Any objections?
14
3473
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys() will produce but it could also...
1
3982
by: bhavanirayala | last post by:
Hi, I am sending the values from one method to another method to get the values from xml file based on the inputs. I am getting the error like:: Variable "$collType" will not stay shared Variable "$collState" will not stay shared at line 77. please see the below code and help me out.
13
12771
by: Nader | last post by:
Hello, I have a dictionary and will get all keys which have the same values. d = {('a' : 1), ('b' : 3), ('c' : 2),('d' : 3),('e' : 1),('f' : 4)} I will something as : d.keys(where their values are the same)
12
5011
by: Florian Brucker | last post by:
Hi everybody! Given a dictionary, I want to create a clustered version of it, collecting keys that have the same value: {1:, 2:, 3:} That is, generate a new dict which holds for each value of the old dict a list of the keys of the old dict that have that very value.
0
9480
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
10327
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
10151
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
8973
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
7499
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
6740
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.