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
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: -
-
>>> d = {'a':1, 'b':2, 'c':3}
-
>>> di = dict((v, k) for k, v in d.items())
-
>>> di
-
{1: 'a', 2: 'b', 3: 'c'}
-
>>>
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
- Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
-
>>> di = dict((v, k) for k, v in d.items())
-
-
Traceback (most recent call last):
-
File "<pyshell#9>", line 1, in -toplevel-
-
di = dict((v, k) for k, v in d.items())
-
TypeError: list objects are unhashable
-
>>>
I am getting the above error when I applied for the example I mentioned
-PSB
bvdet 2,851
Recognized Expert Moderator Specialist - Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
-
>>> di = dict((v, k) for k, v in d.items())
-
-
Traceback (most recent call last):
-
File "<pyshell#9>", line 1, in -toplevel-
-
di = dict((v, k) for k, v in d.items())
-
TypeError: list objects are unhashable
-
>>>
I am getting the above error when I applied for the example I mentioned
-PSB
Try this: - >>> dict(zip([str(x) for x in d.values()], d.keys()))
-
{'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
-
>>>
Lists are not hashable but strings are.
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 -
if [0,1,0] in d.values():
-
print "found "
-
Try this: - >>> dict(zip([str(x) for x in d.values()], d.keys()))
-
{'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
-
>>>
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
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
Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
you can use tuples. -
>>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
-
>>> di = dict((v, k) for k, v in d.items())
-
>>> di
-
{(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
-
>>>
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
Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
you can use tuples. -
>>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
-
>>> di = dict((v, k) for k, v in d.items())
-
>>> di
-
{(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
-
>>>
Good work Barton. I didn't think of tuples. - >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
-
>>> dict(zip([tuple(x) for x in d.values()], d.keys()))
-
{(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
-
>>>
bartonc 6,596
Recognized Expert Expert
Good work Barton. I didn't think of tuples. - >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
-
>>> dict(zip([tuple(x) for x in d.values()], d.keys()))
-
{(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
-
>>>
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...
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mike |
last post by:
Hi,
Does anyone know of reliable programs that convert C# to Java and viceversa?
Thanks
Mike
|
by: SenthilVel |
last post by:
how to get the corresponding values for a given Key in hashtable ??
|
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...
|
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
|
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?
| |
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...
|
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.
|
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)
|
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.
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |