[Chicago] Question about accessing dictionary's elements.
Lewit, Douglas
d-lewit at neiu.edu
Sat Sep 19 20:56:10 CEST 2015
You have any time on Monday? I really gotta get going. If I sit in front
of this laptop any longer, I'm going to go completely crazy! I spent all
morning working on the code for the KNN algorithm, and I'm still not really
done with it! (But pretty close I think.... I hope! )
On Sat, Sep 19, 2015 at 1:54 PM, Joshua Herman <zitterbewegung at gmail.com>
wrote:
> umm i can do skype
>
>
> On Sat, Sep 19, 2015 at 1:46 PM Lewit, Douglas <d-lewit at neiu.edu> wrote:
>
>> Hey Joshua! I would LOVE to, but I have classes on Wednesday and
>> Thursday evenings. So.... ???
>>
>> On Sat, Sep 19, 2015 at 1:30 PM, Joshua Herman <zitterbewegung at gmail.com>
>> wrote:
>>
>>> Hey I'm doing machine learning too why don't you come to one of the
>>> python meet ups in Chicago
>>>
>>> On Sat, Sep 19, 2015 at 12:56 PM Lewit, Douglas <d-lewit at neiu.edu>
>>> wrote:
>>>
>>>> Hi William,
>>>>
>>>> I'm doing a KNN program (K-Nearest-Neighbors). I'm attaching what I've
>>>> done so far. It's not complete! But I've made good progress I think.
>>>> (The program won't work unless you also download the .txt file that the
>>>> program uses to read in the data.)
>>>>
>>>> For this problem the data has to get split into two sets: a training
>>>> set (kind of like a control group in an experiment) and the test set (the
>>>> experimental group or its equivalent). I have to use the K smallest
>>>> normalized distances to make a guess about which class the records in the
>>>> Training Set belong to. (There are six classes: {1, 2, 3, 5, 6, 7}.
>>>> There's no class #4.)
>>>>
>>>> This problem has given me a massive headache, but I think I'm almost
>>>> done with it. All that's left really is to use one of the algorithms to
>>>> make a decision about which class each Test record should be assigned to.
>>>>
>>>> Thanks for the feedback!
>>>>
>>>> Best,
>>>>
>>>> Douglas.
>>>>
>>>>
>>>> On Sat, Sep 19, 2015 at 10:45 AM, William E. S. Clemens <
>>>> wesclemens at gmail.com> wrote:
>>>>
>>>>> 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
>>>>>>
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> 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
>>>
>>>
>> _______________________________________________
>> 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/a641f1d9/attachment.html>
More information about the Chicago
mailing list