[Chicago] Question about accessing dictionary's elements.
Jimmy Calahorrano
jcalahor at yahoo.com
Sat Sep 19 15:09:53 CEST 2015
is more elegant but not sure if is the best option:
[A[key] for key in A.keys() if key[0] == 1]
--------------------------------------------
On Sat, 9/19/15, Lewit, Douglas <d-lewit at neiu.edu> wrote:
Subject: [Chicago] Question about accessing dictionary's elements.
To: "The Chicago Python Users Group" <chicago at python.org>
Date: Saturday, September 19, 2015, 7:43 AM
Hi
guys,
I have a problem and not
sure how to address it. Let's say I have a simple
dictionary such as.
A = { (1, 2) : 10, (1, 3) :
15, (1, 4) : 50, (2, 0) : 2, (2, 5) : 8, (2, 12) : 19
}
I would like to access any
and all tuples whose first value is 1 or let's say 2.
How would I do that?
So what I'm trying to
go for is:
A[(1, placeholder for any
int value)] thus giving me all the values that correspond to
(1, #). Not really sure how to go about this. Unless....
I suppose I could do something like
this:
for key in
A: if list(key)[0]
== 1:
print(A[key]) #### Or whatever I want to do with the
number.
Hmmm.... maybe that would
work, but is there a more "elegant" way to do
this? Is there any "pattern matching" in Python
so that I could do this:
A[(1, #placeholder for any
int )]
Hey, thanks for your
help.
By the way, has anyone seen
the book LEARNING PYTHON by Mark Lutz? Wow! Full of
great information, but the book is HUGE!!! I'll have
serious back problems if I carry that thing around in my
backpack! There's something about small, lightweight
books that I really prefer. (And then there's eBooks,
but that's a horse of a different color in my
opinion.)
Have a great weekend and
I'm looking forward to some ideas on the above
problem. Thanks in advance.
Best,
Douglas
L.
-----Inline Attachment Follows-----
_______________________________________________
Chicago mailing list
Chicago at python.org
https://mail.python.org/mailman/listinfo/chicago
More information about the Chicago
mailing list