Hello,
what algo do you use, when you want to find the dict values from d, with members
of l. Following example:
>>d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10} l = [7,8] found_dic_members = <yourCode> print found_dict_members
[8,9]
Thanks
Alexander 6 1344
En Thu, 08 Mar 2007 05:20:20 -0300, Alexander Eisenhuth
<ne******@stacom-software.deescribió:
what algo do you use, when you want to find the dict values from d, with
members
of l. Following example:
>>d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
>>l = [7,8]
>>found_dic_members = <yourCode>
>>print found_dict_members
[8,9]
found_dic_members = [d[key] for key in l]
--
Gabriel Genellina
On Thu, 08 Mar 2007 09:20:20 +0100, Alexander Eisenhuth wrote:
Hello,
what algo do you use, when you want to find the dict values from d, with members
of l. Following example:
>>d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
>>l = [7,8]
>>found_dic_members = <yourCode>
>>print found_dict_members
[8,9]
It depends.
If all of the values in d are usable as keys (that is, all the values
are hashable, so no lists or sets etc.) then you can build a
reverse-lookup dictionary and use that for fast look-ups.
>>d = {1: 'a', 2: 'b', 3: 'c'} rd = dict(zip(d.values(), d.keys())) rd
{'a': 1, 'c': 3, 'b': 2}
But if you can't do that, you're stuck with a slow search through the
entire dict:
def reverse_search(d, target):
"""Return the first key of dict d that matches the target value."""
for key, value in d.iteritems():
if value == target:
return key
raise KeyError('no key found matching that value')
In both cases though, you have to think about what you want to happen for
duplicated values.
--
Steven D'Aprano
On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
En Thu, 08 Mar 2007 05:20:20 -0300, Alexander Eisenhuth
<ne******@stacom-software.deescribió:
>what algo do you use, when you want to find the dict values from d, with members of l. Following example:
> >>d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10} l = [7,8] found_dic_members = <yourCode> print found_dict_members
[8,9]
found_dic_members = [d[key] for key in l]
*self stares at the line of code*
*self thinks about what he just posted*
*self realises with acute embarrassment that he's jumped to conclusions
and completely misunderstood the Original Poster's question*
Oops! I'll just be slinking away now...
--
Steven D'Aprano
En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
<st***@REMOVEME.cybersource.com.auescribió:
On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
>found_dic_members = [d[key] for key in l]
*self stares at the line of code*
*self thinks about what he just posted*
*self realises with acute embarrassment that he's jumped to conclusions
and completely misunderstood the Original Poster's question*
Oops! I'll just be slinking away now...
LOL! :)
(...now! But when I saw your previous post, I had to check whether it was
*me* who misunderstood the OP answering with a silly one-liner...)
--
Gabriel Genellina
Gabriel Genellina wrote:
En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
<st***@REMOVEME.cybersource.com.auescribió:
>On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
>>found_dic_members = [d[key] for key in l]
*self stares at the line of code*
*self thinks about what he just posted*
*self realises with acute embarrassment that he's jumped to conclusions and completely misunderstood the Original Poster's question*
Oops! I'll just be slinking away now...
LOL! :)
(...now! But when I saw your previous post, I had to check whether it
was *me* who misunderstood the OP answering with a silly one-liner...)
--Gabriel Genellina
I think it was a silly one-liner.
If you look closely at the example, the only mapping of
[7, 8] ==[8, 9]
is
[key, key] ==[value, value]
Look again at the example and consider the positional relationships.
The answer, then, is trivial if not silly:
[d[k] for k in l]
It doesn't appear he as asking for the interesting case:
[value, value] ==[key, key]
Its easy to see how Gabriel was thrown for a loop on this one.
James
Yes it was the silly on-liner ... it was a bit ago I used it last time ... thanks
Gabriel Genellina schrieb:
En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
<st***@REMOVEME.cybersource.com.auescribió:
>On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
>>found_dic_members = [d[key] for key in l]
*self stares at the line of code*
*self thinks about what he just posted*
*self realises with acute embarrassment that he's jumped to conclusions and completely misunderstood the Original Poster's question*
Oops! I'll just be slinking away now...
LOL! :)
(...now! But when I saw your previous post, I had to check whether it
was *me* who misunderstood the OP answering with a silly one-liner...)
--Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Robin Cull |
last post by:
Imagine I have a dict looking something like this:
myDict = {"key 1": , "key
2": , "key 3": ,
"key 4": }
That is, a set of keys which have a variable length list of associated
values after...
|
by: Helmut Jarausch |
last post by:
Hi,
according to the docs the following should work (IMHO)
import csv
csv_file= file('test.csv')
Inp= csv.DictReader(csv_file,,\
lineterminator='\n')
|
by: OPQ |
last post by:
Hi all,
I'd happy to have you share some thougts about ultimate optimisations
on those 2 topics:
(1)- adding one caractere at the end of a string (may be long)
(2)- in a dict mapping a key...
|
by: David Rasmussen |
last post by:
If I have a collection of dicts like:
john = {'id': 1, 'name': "John Cleese", 'year': 1939}
graham = {'id': 2, 'name': "Graham Chapman", 'year': 1941}
I could store all of them in a list. But...
|
by: Stef Mientki |
last post by:
In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin",...
|
by: bearophileHUGS |
last post by:
The PEP 3100:
http://www.python.org/dev/peps/pep-3100/
says:
Return iterators instead of lists where appropriate for atomic type
methods (e.g. dict.keys(), dict.values(), dict.items(), etc.);...
|
by: jeremito |
last post by:
Please excuse me if this is obvious to others, but I can't figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override...
|
by: Seongsu Lee |
last post by:
Hi,
I have a dictionary with million keys. Each value in the
dictionary has a list with up to thousand integers.
Follow is a simple example with 5 keys.
dict = {1: ,
2: ,
900000: ,
900001:...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |