[Chicago] Question about accessing dictionary's elements.

William E. S. Clemens wesclemens at gmail.com
Sat Sep 19 17:45:15 CEST 2015


I would agree that Jimmy's solution is the correct one for doing this. But
I would avoid setting up your data structure in this manner. A dict is a
hash table and it is be extremely fast a looking up a value by key.

You are building an array of the keys to linearly search. This operation is
going to be slow and will not scale well. If you described what this data
is and what your trying todo with it. I maybe able to suggest a better data
structure for storing and accessing it.




--
William Clemens
Phone: 847.485.9455
E-mail: wesclemens at gmail.com

On Sat, Sep 19, 2015 at 10:02 AM, Lewit, Douglas <d-lewit at neiu.edu> wrote:

> I love it!  Thanks Jim.
>
> On Sat, Sep 19, 2015 at 8:09 AM, Jimmy Calahorrano via Chicago <
> chicago at python.org> wrote:
>
>> 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
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> https://mail.python.org/mailman/listinfo/chicago
>>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150919/fb53fcd6/attachment.html>


More information about the Chicago mailing list