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 [email protected] c. (267) 630-0369 (text is okay) f. (267) 352-6298 On Sat, Feb 21, 2015 at 5:00 AM, <[email protected]> wrote: > Send Tutor mailing list submissions to > [email protected] > > 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 > [email protected] > > You can reach the person managing the list at > [email protected] > > 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 <[email protected]> > To: Mark Lawrence <[email protected]> > Cc: [email protected] > 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 <[email protected]> > 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 <[email protected]> > To: Chris Stinemetz <[email protected]> > Cc: Mark Lawrence <[email protected]>, Python Tutor Mailing List > <[email protected]> > Subject: Re: [Tutor] updating a dictionary > Message-ID: > <CAGZAPF5dddoc6cxegF= > [email protected]> > Content-Type: text/plain; charset=UTF-8 > > On Fri, Feb 20, 2015 at 3:47 PM, Chris Stinemetz > <[email protected]> wrote: > > > > 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. > > > Ah. Be wary of structure sharing when the values being shared are mutable. > > > A textbook example of this would be: > > ########################################### > message = ['hello', 'world'] > copy = message > copy.append('!') > print copy > print message > ## What do we expect to see here? What do we see? > ########################################### > > The code above here is wrong to use the word "copy" here, because it's > not copying the structure at all. copy refers to the *same* list > value. Mutations to the value will be observable when we access that > list through either 'message' or 'copy', since fundamentally they're > both referring to the same list value. > > To copy a list, we can use a whole slice: > > ##################### > message = ['hello', 'world'] > copy = message[:] > copy.append('!') > print copy > print message > ##################### > > > I don't know what your program looks like at this point, so I can't > pinpoint exactly where this is happening, but at the very least, this > should help you figure out what's going on. > > Feel free to ask if you'd like more explanation. If you'd like a > visualization, also see: > > http://pythontutor.com/visualize.html#code=message+%3D+%5B'hello > ',+'world'%5D%0Acopy+%3D+message%0Acopy.append('!')&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=2&rawInputLstJSON=%5B%5D&curInstr=0 > > and compare vs: > > http://pythontutor.com/visualize.html#code=message+%3D+%5B'hello > ',+'world'%5D%0Acopy+%3D+message%5B%3A%5D%0Acopy.append('!')&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=2&rawInputLstJSON=%5B%5D&curInstr=0 > > > Good luck! > > > ------------------------------ > > Message: 3 > Date: Sat, 21 Feb 2015 13:58:20 +1100 > From: Steven D'Aprano <[email protected]> > To: [email protected] > Subject: Re: [Tutor] subprocess outputing wrong info to command line > Message-ID: <[email protected]> > Content-Type: text/plain; charset=us-ascii > > On Fri, Feb 20, 2015 at 06:58:17PM -0500, brads wrote: > > > My subprocess is in error but the same command at the command line works > > fine. > > Are you running the Python script as the same user and from the same > location as the command line? If you are running the Python script as an > unprivileged user from your home directory, and the command line as root > from the directory holding the key, then it is not surprising that they > will get different results. > > Does the dnssec-signzone command use any environment variables? Perhaps > they are not being inherited by the Python subprocess. > > Also, a note about sending code to the list. For some reason, every line > in your code is separated by blank lines: > > > # cat makekeys.py > > > > #!/usr/bin/python3.4 > > > > import subprocess > > > > import sys > > etc. That makes it very hard to read. Please fix that. > > Secondly, you have a huge amount of extraneous code which is irrelevant > to your problem with subprocess. You calculate dates, ask the user for > input, delete files, and write new files, calculate arrays and more. > None of that has anything to do with subprocess. Take it out. Reduce > your problem to the smallest possible code which shows the problem. 9 > times out of 10, the process of cutting your code down to manageable > size will actually help you to solve your own problem, and the other 1 > time out of 10 we have a smaller and less confusing piece of code to try > to understand. > > You also have dead, obsolete code commented out. Comments should not be > used for keeping history in the file, comments should be used for > commenting about the code. Best practice is to use a revision control > system like hg, but if you're not up to that at least delete the dead > code before posting. > > When running into a problem with subprocess, your first step should be > to *exactly* duplicate the successful command: > > dnssec-signzone -e20180330000000 -p -t -g -k Ktest123.com.ksk.key -o > test123.com test123.com.external Ktest123.com.zsk.key > > > So you should try running that from subprocess: > > subprocess.call([ > 'dnssec-signzone', '-e20180330000000', '-p', '-t', '-g', > '-k', 'Ktest123.com.ksk.key', '-o', 'test123.com', > 'test123.com.external', 'Ktest123.com.zsk.key', > ]) > > > and see if it still works. If it still does not work, that strongly > suggests a problem with the environment: you are running it as the wrong > user, in the wrong location, missing enviromnent variables or > permissions. If it works in Python, then you can start replacing each > constant argument with a calculated argument, *one argument at a time*, > and see where you introduce the bug. > > > > -- > Steve > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - [email protected] > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 132, Issue 51 > ************************************** > _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
