[Chicago] Question about accessing dictionary's elements.

Lewit, Douglas d-lewit at neiu.edu
Sat Sep 19 14:43:35 CEST 2015


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150919/dcc1b8f0/attachment.html>


More information about the Chicago mailing list