[Tutor] Help Command Question
Hi Everyone, I'm trying to print a command of list options by using the help command in the iPython interpreter. Read captured copy of the printout as follows: 'Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v. 1500 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 3.2.0 -- An enhanced Interactive Python. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org In [1]: help list File "", line 1 help list ^ SyntaxError: invalid syntax.' Question: What is the correct help command? Regards, Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] line error on no. 7
Hi Everyone, I'm writing python code to read a data text file, split the file into a list of words using the split(function) and to print the results in alphabetical order. The raw python code is located at http://tinyurl.com/oua9uqx The sample data is located at http://tinyurl.com/odt9nhe Desired Output: ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder'] There is a line error on no. 7 What is the cause of this error? Regards, Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] String Attribute
Hi Everyone: What is the source of the syntax error to the String Attribute? Go to the following URL links and view a copy of the raw data file code and sample data: 1.) http://tinyurl.com/p2xxxhl 2.) http://tinyurl.com/nclg6pq Here is the desired output: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] line error on no. 7
Thanks, I’ll need some time to review your notes Sent from Surface From: Martin A. Brown Sent: Tuesday, July 28, 2015 4:41 PM To: ltc.hots...@gmail.com Cc: Tutor@python.org Hello again, > The raw python code is located at http://tinyurl.com/oua9uqx It is not very long, so you can post inline (I pasted it below). If you have a longer piece of code, then, yes, a pastebin is a good choice. (Also, if in the future, you have a more complex piece of code, try to simplify your posting to just the part that is giving you trouble.) But, your question is clear here. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: if fh == list: continue list.split() list.append sorted("fh") print line.rstrip() You have confused your names in line 7. list.split() There are a few lessons to learn here. * Are you operating on the variable you intend to be operating on? No, in this case. You wanted to do something like 'lst.split()' * Did you want to throw away the result of the call to .split()? I'm guessing the answer is 'No.' So, you'll need another variable to hold the value from lst.split(). Line 7 should become: lst.extend(line.split()) Additional comments: * Your line 6 performs a test to see if fh (file object referring to the stanza) equals the builtin called 'list'. That doesn't make sense. Try typing list and list() at an interactive prompt, and you may see that it doesn't make sense to compare those things. >>> list >>> list() [] The first tells you what 'list' is. The second calls 'list()', which returns you, well... a new Python list object. * Your line 8 is a NOOP (no-operation). In that line, you are simply referring to a method on the builtin list type. Use the interactive interpreter to see what I mean: >>> * Your line 9 doesn't make much sense to me. It may no longer be a syntax error, but it isn't doing what you think it's doing. Try it out in the interactive interpreter to see what it's doing: >>> sorted("fh") ['f', 'h'] OK, so now, let me make a few suggestions. Others may have additional comments, but I'd be interested in seeing how you adjust your program after working through the above. Thus, I suggest removing most of the loop internals and rewriting. Everything else is a good start. I'll suggest a possible ending, too. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: # Code is good until this point. # Here, you want to find the words and add them to the list # called 'lst'. # -- now, outside the loop, I would suggest printing the list print lst Once you can run your program and get something other than an empty list, you know you have made progress. Good luck with your iambic pentameter, -Martin > The sample data is located at > http://tinyurl.com/odt9nhe Also, I'm including your short data sample: But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] mbox-short
Hi Everyone, How do I file in the empty list at 0 on line # 3 to produce the desired output: The first goal of the program is to produce an output from the date list file as following: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu zq...@umich.edu rjl...@iupui.edu zq...@umich.edu rjl...@iupui.edu c...@iupui.edu c...@iupui.edu And secondly, print out a count at the end Raw Python code is available at http://tinyurl.com/p4k8qa4 The data input file is available at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Mailbox
Hi Everyone: I have a second and unrelated question: I tried to work backward to see if there is a logic error associated with a variable is being skipped over: #top of code, initialize variable output_list = ["default"] #rest of code If you get at the end print output_list ['default'] Raw Data File: count = 0 fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" for line in fname: line = line.strip() if not line.startswith('From '): continue line = line.split() count = count + 1 print len(line) fh = open(fname) print "There were", count, "lines in the file with From as the first word" Sample data file at http://www.pythonlearn.com/code/mbox-short.txt Desired Output: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu zq...@umich.edu rjl...@iupui.edu zq...@umich.edu rjl...@iupui.edu Thanks, Hal Sent from Surface From: Phu Sam Sent: Wednesday, July 29, 2015 1:06 PM To: Ltc Hotspot ___ Baypiggies mailing list baypigg...@python.org To change your subscription options or unsubscribe: https://mail.python.org/mailman/listinfo/baypiggies ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 11:47 AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.split()[0]: count += 1 print "There are %d lines starting with From" % count print len(line) fn = open(fname) print "There were", count, "lines in the file with From as the first word" Syntax message produced by iPython interperter: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in () 6 print "There are %d lines starting with From" % count 7 print len(line) > 8 fn = open(fname) 9 print "There were", count, "lines in the file with From as the first wor d" NameError: name 'fname' is not defined In [16]: Question: Why is fname = "mbox-short.txt" not loading the sample data? Sample data file is located at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface From: Steven D'Aprano Sent: Wednesday, July 29, 2015 7:42 AM To: ltc.hots...@gmail.com Cc: Tutor@python.org On Tue, Jul 28, 2015 at 11:33:53PM +, ltc.hots...@gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and > sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Hi everyone, Revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] fn.seek(0) print sender Questions: Why is the loop not repeating, and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' From stephen.marqu...@uct.ac.za Sat Jan 5 09:14:16 2008 ← Mismatch Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 12:07 PM To: Tutor@python.org Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 11:47 AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.split()[0]: count += 1 print "There are %d lines starting with From" % count print len(line) fn = open(fname) print "There were", count, "lines in the file with From as the first word" Syntax message produced by iPython interperter: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in () 6 print "There are %d lines starting with From" % count 7 print len(line) > 8 fn = open(fname) 9 print "There were", count, "lines in the file with From as the first wor d" NameError: name 'fname' is not defined In [16]: Question: Why is fname = "mbox-short.txt" not loading the sample data? Sample data file is located at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface From: Steven D'Aprano Sent: Wednesday, July 29, 2015 7:42 AM To: ltc.hots...@gmail.com Cc: Tutor@python.org On Tue, Jul 28, 2015 at 11:33:53PM +, ltc.hots...@gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and > sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
sure Sent from Surface From: Mark Lawrence Sent: Thursday, July 30, 2015 3:25 PM To: Tutor@python.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Hi Mark, I’m still confused because line 4 reads: fh=open(fname,'r') # Open a new file handle, not fn = open(fname) Therefore, can you write down line by line from error to correction? Here is the revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[2] print sender Regards, Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] 'open' is not defined
Hi Everyone: Why is open not defined in the following code:NameError: name 'open' is not defined Code reads as follows: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" Regards, Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Hi Alan, I rewrote the code as follows: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" Question: How do I remove the duplicates: stephen.marqu...@uct.ac.za stephen.marqu...@uct.ac.za← Mismatch lo...@media.berkeley.edu lo...@media.berkeley.edu zq...@umich.edu zq...@umich.edu rjl...@iupui.edu rjl...@iupui.edu Regards, Hal Sent from Surface From: Alan Gauld Sent: Thursday, July 30, 2015 5:04 PM To: Tutor@python.org On 30/07/15 22:17, ltc.hots...@gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" # assign fname > fh=open(fname,'r') # Open a new file handle > for line in fh: > print line > if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] > fn.seek(0) > print sender > > Questions: Why is the loop not repeating, What makes you think so? >>No count = count +1 If you get an error(as I suspect) please post the entire error message. >>OK I would expect a name error on the last line of the loop since there is no variable fn defined. I don't know what you think the seek() is doing, but (assuming you meant fh) it will reset the file to the first line each time so you never finish the loop. >>OK > and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' > > From stephen.marqu...@uct.ac.za Sat Jan 5 09:14:16 2008 ← Mismatch Splitting on whitespace will ensure the bit you want is in the second element >>Check the revised code, above -- 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'open' is not defined
Hi Emile, I hope this answers your question? Question: How do I remove each duplicate line output? Here is the raw data code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" The problem is in the output results: Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 3.2.0 -- An enhanced Interactive Python. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: cd C:\Users\vm\Desktop\apps\docs\Python C:\Users\vm\Desktop\apps\docs\Python In [2]: %run _8_5_v_7.py Enter file name: mbox-short.txt stephen.marqu...@uct.ac.za stephen.marqu...@uct.ac.za lo...@media.berkeley.edu lo...@media.berkeley.edu zq...@umich.edu zq...@umich.edu rjl...@iupui.edu rjl...@iupui.edu zq...@umich.edu zq...@umich.edu rjl...@iupui.edu rjl...@iupui.edu c...@iupui.edu c...@iupui.edu c...@iupui.edu c...@iupui.edu gsil...@umich.edu gsil...@umich.edu gsil...@umich.edu gsil...@umich.edu zq...@umich.edu zq...@umich.edu gsil...@umich.edu gsil...@umich.edu wagne...@iupui.edu wagne...@iupui.edu zq...@umich.edu zq...@umich.edu antra...@caret.cam.ac.uk antra...@caret.cam.ac.uk gopal.ramasammyc...@gmail.com gopal.ramasammyc...@gmail.com david.horw...@uct.ac.za david.horw...@uct.ac.za david.horw...@uct.ac.za david.horw...@uct.ac.za david.horw...@uct.ac.za david.horw...@uct.ac.za david.horw...@uct.ac.za david.horw...@uct.ac.za stephen.marqu...@uct.ac.za stephen.marqu...@uct.ac.za lo...@media.berkeley.edu lo...@media.berkeley.edu lo...@media.berkeley.edu lo...@media.berkeley.edu r...@media.berkeley.edu r...@media.berkeley.edu c...@iupui.edu c...@iupui.edu c...@iupui.edu c...@iupui.edu c...@iupui.edu c...@iupui.edu There were 54 lines in the file with From as the first word In [3]: Regards, Hal ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Hi Alan, Here is the revised code below: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] addresses = set() addresses.add(line4) count = count + 1 print addresses print "There were", count, "lines in the file with From as the first word" The code produces the following out put: In [15]: %run _8_5_v_13.py Enter file name: mbox-short.txt set(['stephen.marqu...@uct.ac.za']) set(['stephen.marqu...@uct.ac.za']) set(['lo...@media.berkeley.edu']) set(['lo...@media.berkeley.edu']) set(['zq...@umich.edu']) set(['zq...@umich.edu']) set(['rjl...@iupui.edu']) set(['rjl...@iupui.edu']) set(['zq...@umich.edu']) set(['zq...@umich.edu']) set(['rjl...@iupui.edu']) set(['rjl...@iupui.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) set(['gsil...@umich.edu']) set(['gsil...@umich.edu']) set(['gsil...@umich.edu']) set(['gsil...@umich.edu']) set(['zq...@umich.edu']) set(['zq...@umich.edu']) set(['gsil...@umich.edu']) set(['gsil...@umich.edu']) set(['wagne...@iupui.edu']) set(['wagne...@iupui.edu']) set(['zq...@umich.edu']) set(['zq...@umich.edu']) set(['antra...@caret.cam.ac.uk']) set(['antra...@caret.cam.ac.uk']) set(['gopal.ramasammyc...@gmail.com']) set(['gopal.ramasammyc...@gmail.com']) set(['david.horw...@uct.ac.za']) set(['david.horw...@uct.ac.za']) set(['david.horw...@uct.ac.za']) set(['david.horw...@uct.ac.za']) set(['david.horw...@uct.ac.za']) set(['david.horw...@uct.ac.za']) set(['david.horw...@uct.ac.za']) set(['david.horw...@uct.ac.za']) set(['stephen.marqu...@uct.ac.za']) set(['stephen.marqu...@uct.ac.za']) set(['lo...@media.berkeley.edu']) set(['lo...@media.berkeley.edu']) set(['lo...@media.berkeley.edu']) set(['lo...@media.berkeley.edu']) set(['r...@media.berkeley.edu']) set(['r...@media.berkeley.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) set(['c...@iupui.edu']) There were 54 lines in the file with From as the first word Question no. 1: is there a build in function for set that parses the data for duplicates. In [18]: dir (set) Out[18]: ['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update'] Question no. 2: Why is there not a building function for append? Question no. 3: If all else fails, i.e., append & set, my only option is the slice the data set? Regards, Hal Sent from Surface From: Alan Gauld Sent: Friday, July 31, 2015 2:00 AM To: Tutor@python.org On 31/07/15 01:25, ltc.hots...@gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: > if not line.startswith('From'): continue > line2 = line.strip() > line3 = line2.split() > line4 = line3[1] > print line4 > count = count + 1 > print "There were", count, "lines in the file with From as the first word" > > Question: How do I remove the duplicates: OK, You now have the original code working, well done. To remove the duplicates you need to collect the addresses rather than printing them. Since you want the addresses to be unique you can use a set. You do that by first creating an empty set above the loop, let's call it addresses: addresses = set() Then replace your print statement with the set add() method: addresses.add(line4) This means that at the end of your loop you will have a set containing all of the unique addresses you found. You now print the set. You can do that directly or for more control over layout you can write another for loop that prints each address individually. print addresses or for address in addresses: print address # plus any formatting you want You can also sort the addresses by calling the sorted() function before printing: print sorted(addresses) 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
Re: [Tutor] String Attribute
Hi Martin, Hal is not have a great day, indeed to day: Here is the raw data entered: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: line2 = line.strip() line3 = line2.split() line4 = line3[0] addresses.add(line4) count = count + 1 print "There were", count, "lines in the file with From as the first word" print addresses →Question: Why is the list index out of range on line # 9: IndexError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_20.py in () 7 line2 = line.strip() 8 line3 = line2.split() > 9 line4 = line3[1] 10 addresses.add(line4) 11 count = count + 1 IndexError: list index out of range →I entered different index ranges from [] to [5] that, later, produced the same Index Error message: IndexError: list index out of range In [34]: print line3[] File "", line 1 print line3[] ^ SyntaxError: invalid syntax In [35]: print line[1] --- IndexErrorTraceback (most recent call last) in () > 1 print line[1] IndexError: string index out of range In [36]: print line[2] --- IndexErrorTraceback (most recent call last) in () > 1 print line[2] IndexError: string index out of range In [37]: print line[3] --- IndexErrorTraceback (most recent call last) in () > 1 print line[3] IndexError: string index out of range In [38]: print line[4] --- IndexErrorTraceback (most recent call last) in () > 1 print line[4] IndexError: string index out of range In [39]: print line[5] --- IndexErrorTraceback (most recent call last) in () > 1 print line[5] IndexError: string index out of range →Question: I think the problem is in the placement of the address set: The addresses = set()? Regards, Hal Sent from Surface From: Martin A. Brown Sent: Friday, July 31, 2015 9:18 AM To: ltc.hots...@gmail.com Cc: Tutor@python.org Greetings again Hal, Thank you for posting your small amounts of code and results inline. Thanks for also including clear questions. Your "surface" still seems to add extra space, so, if you could trim that, you may get even more responses from others who are on the Tutor mailing list. Now, on to your question. > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: >if not line.startswith('From'): continue >line2 = line.strip() >line3 = line2.split() >line4 = line3[1] >addresses = set() >addresses.add(line4) >count = count + 1 >print addresses > print "There were", count, "lines in the file with From as the first word" > The code produces the following out put: > > In [15]: %run _8_5_v_13.py > Enter file name: mbox-short.txt > set(['stephen.marqu...@uct.ac.za']) [ ... snip ... ] > set(['c...@iupui.edu']) > > Question no. 1: is there a build in function for set that parses > the data for duplicates. The problem is not with the data structure called set(). Your program is not bad at all. I would suggest making two small changes to it. I think I have seen a pattern in the samples of code you have been sending--this pattern is that you reuse the same variable inside a loop, and do not understand why you are not collecting (or accumulating) all of the results. Here's your program. I have moved two lines. The idea here is to initialize the 'addresses' variable before the loop begins (exactly like you do with the 'count' variable). Then, after the loop completes (and, you have processed all of your input and accumulated all of the desired data), you can also print out the contents of the set variable called 'addresses'. Try this out: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] addresses.add(line4) count = count + 1 print "There were", count, "lines in the file with From as the first word" print addresses > Question no. 2: Why is there not a building function for append? >> Alan answered the question, thanks > Question no. 3: If all else fails
Re: [Tutor] String Attribute
Emile, --> Captured is a printout from line3 to addresses, below: In [46]: print line3 [] In [47]: print line2.split() [] In [48]: print line2 In [49]: print line.strip() In [50]: print fh In [51]: print addresses set(['1.0', 'sou...@collab.sakaiproject.org;', 'Jan', 'mail.umich.edu', 'Innocen t', '0.', 'CMU', 'frankenstein.mail.umich.edu', '0.8475', 'from', 'source@co llab.sakaiproject.org', '05', '<200801051412.m05eciah010...@nakamura.uits.iupui. edu>', 'flawless.mail.umich.edu', '5', 'nakamura.uits.iupui.edu:', 'shmi.uhi.ac. uk', '7bit', 'text/plain;', ';', 'Sat,', 'nakamu ra.uits.iupui.edu', 'paploo.uhi.ac.uk', 'FROM', 'holes.mr.itd.umich.edu', '(from ', '', '[sakai]', 'stephen.marqu...@uct.ac.z a', 'Sat']) In [52]: →Original Traceback error message: IndexError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\_8_5_v_21.py in () 7 line2 = line.strip() 8 line3 = line2.split() > 9 line4 = line3[1] 10 addresses.add(line4) 11 count = count + 1 IndexError: list index out of range → Latest code printout: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: line2 = line.strip() line3 = line2.split() line4 = line3[1] addresses.add(line4) count = count + 1 print "There were", count, "lines in the file with From as the first word" print addresses → I swapped line statements between lines 4 & 5. No change on the Traceback error message: IndexError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\_8_5_v_21.py in () 8 line2 = line.strip() 9 line3 = line2.split() ---> 10 line4 = line3[1] 11 addresses.add(line4) 12 count = count + 1 IndexError: list index out of range In [54]: print line3 →Question: The file data content is lost on execution of the sort function? Regards, Hal Sent from Surface From: Emile van Sebille Sent: Friday, July 31, 2015 4:16 PM To: Tutor@python.org On 7/31/2015 11:57 AM, ltc.hots...@gmail.com wrote: > →Question: Why is the list index out of range on line # 9: > > IndexError > > Traceback (most recent call last) > C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_20.py in () >7 line2 = line.strip() >8 line3 = line2.split() > > 9 line4 = line3[1] > 10 addresses.add(line4) > 11 count = count + 1 > IndexError: list index out of range > Because line3 is not sub-scriptable. Have you examined what line3 holds when the error occurs? Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Sent from Surface From: Alan Gauld Sent: Friday, July 31, 2015 4:54 PM To: Tutor@python.org On 31/07/15 19:57, ltc.hots...@gmail.com wrote: > for line in fh: > line2 = line.strip() > line3 = line2.split() > line4 = line3[0] You need to check that there actually is something in the list to access. If you get a line with only one word in it, or even a blank line this will fail. →→Apparently, the data content in the file is lost from the address sort function to line2? : In [46]: print line3 [] In [47]: print line2.split() [] In [48]: print line2 In [49]: print line.strip() In [50]: print fh In [51]: print addresses set(['1.0', 'sou...@collab.sakaiproject.org;', 'Jan', 'mail.umich.edu', 'Innocen t', '0.', 'CMU', 'frankenstein.mail.umich.edu', '0.8475', 'from', 'source@co llab.sakaiproject.org', '05', '<200801051412.m05eciah010...@nakamura.uits.iupui. edu>', 'flawless.mail.umich.edu', '5', 'nakamura.uits.iupui.edu:', 'shmi.uhi.ac. uk', '7bit', 'text/plain;', ';', 'Sat,', 'nakamu ra.uits.iupui.edu', 'paploo.uhi.ac.uk', 'FROM', 'holes.mr.itd.umich.edu', '(from ', '', '[sakai]', 'stephen.marqu...@uct.ac.z a', 'Sat']) In [52]: → Latest code printout: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: line2 = line.strip() line3 = line2.split() line4 = line3[1] addresses.add(line4) count = count + 1 print "There were", count, "lines in the file with From as the first word" print addresses > addresses.add(line4) > count = count + 1 > print "There were", count, "lines in the file with From as the first word" Despite what you print you don't know that its true anymore. You have removed the code that tested for the first word being "From". You should put that check back in your code. > →I entered different index ranges from [] to [5] I'm not sure what [] means in this case? It should be a syntax error as you show below. > In [34]: print line3[] >File "", line 1 > print line3[] > ^ > SyntaxError: invalid syntax →→ OK See, that's not an IndexError. They are different and have different causes. A syntax error means your code is not valid Python. An IndexError means the code is valid but its trying to access something that doesn't exist. →→ OK →Question: I think the problem is in the placement of the address set: The addresses = set()? No it has nothing to do with that. The set is not involved in this operation at this point. To debug these kinds of errors insert a print statement above the error line. In this case: print line3 →→ Read printout above That will show you what the data looks like and you can tell whether line3[1] makes any kind of sense. →→id. -- 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor