Re: [Tutor] Tutor Digest, Vol 132, Issue 51
Hi Guys, Very simple question, I imagine. this code throws of off a counter not defined error. Can you help? *def word_counter(word, string):* *counter = 0* *for item in string:* *if item == word:* *counter = counter + 1* *print counter* Thanks, Tim -- Tim Johnson pavemen...@gmail.com c. (267) 630-0369 (text is okay) f. (267) 352-6298 On Sat, Feb 21, 2015 at 5:00 AM, tutor-requ...@python.org wrote: Send Tutor mailing list submissions to tutor@python.org To subscribe or unsubscribe via the World Wide Web, visit https://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-requ...@python.org You can reach the person managing the list at tutor-ow...@python.org When replying, please edit your Subject line so it is more specific than Re: Contents of Tutor digest... Today's Topics: 1. Re: updating a dictionary (Chris Stinemetz) 2. Re: updating a dictionary (Danny Yoo) 3. Re: subprocess outputing wrong info to command line (Steven D'Aprano) -- Message: 1 Date: Fri, 20 Feb 2015 17:47:29 -0600 From: Chris Stinemetz chrisstinem...@gmail.com To: Mark Lawrence breamore...@yahoo.co.uk Cc: tutor@python.org Subject: Re: [Tutor] updating a dictionary Message-ID: ca+hbpzjje-qztvl0hdrt_umatam7c8fwswhkod6gweetvfm...@mail.gmail.com Content-Type: text/plain; charset=UTF-8 On Fri, Feb 20, 2015 at 4:51 PM, Mark Lawrence breamore...@yahoo.co.uk wrote: On 20/02/2015 17:56, Chris Stinemetz wrote: Please don't top post as it makes long threads difficult if not impossible to follow, thanks. I am getting closer. I think I have figured out the logic. I just have a quick question. How do you access key:values in a nested dictionary? MOL02997_C': [{'2': '0', '7': '0', '8': '0', '9': '0'}]} say I want to access the key:value 8:0 print dict['MOL02997_C']['8'] doesn't seem to work. doesn't seem to work doesn't tell us much, so normally you would post your code and the full traceback that you get. However what you have seems to be a dictionary that you've called dict, hence overriding the Python built-in name. This isn't illegal but it's certainly frowned upon. For the key 'MOL02997_C' you have a list which holds one dict which contains a value '8' amongst others. Hence:- mystruct = {'MOL02997_C': [{'2': '0', '7': '0', '8': '0', '9': '0'}]} mystruct {'MOL02997_C': [{'7': '0', '8': '0', '2': '0', '9': '0'}]} mystruct['MOL02997_C'] [{'7': '0', '8': '0', '2': '0', '9': '0'}] mystruct['MOL02997_C'][0] {'7': '0', '8': '0', '2': '0', '9': '0'} mystruct['MOL02997_C'][0]['8'] '0' Got that? ? Thank you Mark. I understand what you are explaining to me but I am not sure why every instance of the key 8:value changes when I assign a new value to it. I am expecting only vals['KSL04523_A'][0]['8'] value to change to 55.55 but as you can see bellow all rows in the dictionary are changes for key 8: Thank you in advance vals['KSL04523_A'] [{'7': '0', '9': '0', '8': '0', '2': '0'}] vals['KSL04523_A'][0] {'7': '0', '9': '0', '8': '0', '2': '0'} vals['KSL04523_A'][0]['8'] '0' vals['KSL04523_A'][0]['8'] = 55.55 pprint.pprint(vals) {'CELL_': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04514_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04514_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04515_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04515_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04515_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04516_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04516_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04516_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04517_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04517_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04517_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04519_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04519_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04519_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04520_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04520_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04520_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04521_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04521_B': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04521_C': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}], 'KSL04523_A': [{'2': '0', '7': '0', '8': 55.55, '9': '0'}]}? -- Message: 2 Date: Fri, 20 Feb 2015 17:43:23 -0800 From: Danny Yoo d...@hashcollision.org To: Chris Stinemetz chrisstinem...@gmail.com Cc: Mark Lawrence breamore...@yahoo.co.uk, Python Tutor Mailing
Re: [Tutor] Tutor Digest, Vol 132, Issue 51
On Feb 21, 2015 8:49 AM, Tim Johnson pavemen...@gmail.com wrote: Hi Guys, Very simple question, I imagine. this code throws of off a counter not defined error. Can you help? *def word_counter(word, string):* *counter = 0* *for item in string:* *if item == word:* *counter = counter + 1* *print counter* Hmmm... I don't understand what the intent of the last line of the program is. Can you explain what you're trying to do there? Do you intend to do this print after the for loop? If so, it should still be scoped to the function though. At the moment, it is a statement that's entirely separate from the function definition. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counter not defined error
On 21/02/2015 16:48, Tim Johnson wrote: I've changed the subject line to reflect your question. Did you have to reply to the digest and include the lot of it, why couldn't you simply start a new thread? Hi Guys, Very simple question, I imagine. this code throws of off a counter not defined error. Can you help? *def word_counter(word, string):* *counter = 0* *for item in string:* *if item == word:* *counter = counter + 1* *print counter* Tim Johnson counter only exists within the scope of the word_counter function, the print statement is outside of that scope. Shifting the print statement right four spaces should do the trick. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 132, Issue 51
On 21/02/15 16:48, Tim Johnson wrote: Hi Guys, Hi, please don't send the entire digest, some folks pay by the byte. Ideally send a new mail to tutor@python.org instead of replying to an existing digest. Also change the subject to something meaningful so folks an find it in the archives. *def word_counter(word, string):* *counter = 0* *for item in string:* *if item == word:* *counter = counter + 1* *print counter* Your last line is outside the function so counter is invisible to it. You need to indent the line so it is in line with the for... line However I suspect this function is not going to do what you hope it will. item will iterate over the characters in the string not the words. You probably want something like def word)_counter(word, string): count = 0 for group in string.split(): if group == word: count += 1 return count hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] NameError: name 'counter' is not defined, was Re: Tutor Digest, Vol 132, Issue 51
Tim Johnson wrote: Hi Guys, Hi Tim! Very simple question, I imagine. this code throws of off a counter not defined error. Instead of retyping the error message it is better to cut and paste the traceback, e. g. Traceback (most recent call last): File tmp.py, line 6, in module print counter NameError: name 'counter' is not defined Can you help? def word_counter(word, string): counter = 0 for item in string: if item == word: counter = counter + 1 print counter When you assign a value to a variable inside a function that variable is only known inside that function and only during the execution of that function. When you move the print statement into the function it should work as the following session in the interactive interpreter demonstrates: def word_counter(word, string): ... counter = 0 ... for item in string: ... if item == word: ... counter = counter + 1 ... print counter ... word_counter(the, the house by the sea) 0 Oops! by default a for loop iterates over the characters in a string, not the words. Let's try with a list of words: word_counter(the, [the, house, by, the, sea]) 2 If you want to allow a string argument you can modify your function to iterate over for item in string.split(): ... Another improvement would be to have the function return the counter instead of printing it. That way you can do other things with it: def word_counter(word, string): ... counter = 0 ... for item in string.split(): ... if item == word: ... counter = counter + 1 ... return counter ... phrase = the house by the sea num_words = len(phrase.split()) print 100.0 * word_counter(the, phrase) / num_words, percent of all words are 'the' 40.0 percent of all words are 'the' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor