On Thu, Aug 6, 2015 at 3:00 PM, Alan Gauld <alan.ga...@btinternet.com> wrote:
> On 06/08/15 19:30, Ltc Hotspot wrote: > > I moved counter outside the loop and below dict, maxval = None >> maxkee = None are both positioned outside the loop. >> > > You moved counter but it is still a dict() and you > don't use it anywhere. > > URL link to the revisions are available at http://tinyurl.com/nvzdw8k >> >> Question: How do I define Counter >> > > Counter is defined for you in the collections module. > So to use it you need to import collections and access it as > collections.Counter. > > But did you read how to use it? It is a lot more than > just a dictionary, it has many extra methods, some of > which almost solve your problem for you. (Whether your > teacher will approve of using Counter is another > issue!) > > Revised code reads: >> fname = raw_input("Enter file name: ") >> handle = open (fname, 'r') >> >> counter = dict () >> c = Counter(['address']) >> > > You only need to pass a list if you are adding multiple things. > > But by the same token you can add a list of items, such > as email addresses. So if you had such a list you could > create a Counter() to hold them and count them for you. > And return the one with the highest value. > Sound familiar? > > Please (re)read the Counter documentation. > Then play with one in the >>> prompt. > Don't expect us to just provide you with code, learn > how it works for yourself. Experiment. > > The >>> prompt is your friend. You will learn more from that in 15 minutes > than in a bunch of emails showing other peoples > code. > > Alternatively forget about Counter and just go back to > your dict(). You have written all the code you need already, > you just need to assemble it in the correct order. > > maxval = None >> maxkee = None >> >> for line in handle: >> if line.startswith("From: "): >> address = line.split()[1] >> > > You are not storing the addresses anywhere. > > for maxkee, val in c.items(): >> >> maxval = val >> maxkee = kee >> > > You are still not testing if its the maximum, > you just keep overwriting the variables for > each element. > > print maxkee and maxval >> > > You still have an 'and' in there. > > -- > 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 > Hi Alan, Questions(1):Why does print line, prints blank space; and, (2) print address prints a single email address: View print results as follows: In [70]: %run assignment_9_4_24.py Enter file name: mbox-short.txt r...@media.berkeley.edu 1 In [71]: print handle <open file 'mbox-short.txt', mode 'r' at 0x00000000035576F0> In [72]: print count {'gopal.ramasammyc...@gmail.com': 1, 'lo...@media.berkeley.edu': 3, 'cwen@iupui. edu': 5, 'antra...@caret.cam.ac.uk': 1, 'rjl...@iupui.edu': 2, 'gsil...@umich.ed u': 3, 'david.horw...@uct.ac.za': 4, 'wagne...@iupui.edu': 1, ' zq...@umich.edu': 4, 'stephen.marqu...@uct.ac.za': 2, 'r...@media.berkeley.edu': 1} In [73]: print line In [74]: print address c...@iupui.edu Question(3): why did the elements print count('keys') and print count('items') fail? View print commands as follows: In [75]: dir (count) Out[75]: ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] In [76]: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-76-35c8707b256e> in <module>() ----> 1 print count('items') TypeError: 'dict' object is not callable In [77]: print count('keys') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-77-54ed4a05a3c7> in <module>() ----> 1 print count('keys') TypeError: 'dict' object is not callable In [78]: Regards, Hal _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor