AttributeError

MRAB python at mrabarnett.plus.com
Wed Aug 12 17:09:06 CEST 2015


On 2015-08-12 04:05, Ltc Hotspot wrote:
> Chris,
>
> Check the code and the visualize execution of the code, available at
> http://tinyurl.com/p8tgd5h
>
> message reads: NameError: name 'collections' is not defined
>
You didn't import the module.

>
> Regards,
> Hal
>
> On Tue, Aug 11, 2015 at 5:49 PM, Chris Angelico <rosuav at gmail.com
> <mailto:rosuav at gmail.com>> wrote:
>
>     On Wed, Aug 12, 2015 at 10:43 AM, Ltc Hotspot <ltc.hotspot at gmail.com
>     <mailto:ltc.hotspot at gmail.com>> wrote:
>     > Python can pull the hour from the 'From ' line by finding the time and then
>     > splitting the string a second time using a colon, i.e., From
>     >stephen.marquard at uct.ac.za <mailto:stephen.marquard at uct.ac.za> Sat
>     Jan  5 09:14:16 2008
>     >
>     > Finally, accumulated the counts for each hour, print out the counts, sorted
>     > by hour as shown below:
>
>     In that case, you want to sort the entire collection, not a single
>     key-value pair.
>
>     It seems to me you can do this fairly efficiently with
>     collections.Counter.
>
>     import collections
>     with open(raw_input("Enter file name: ")) as f:
>          counts = collections.Counter(line.split()[5].rstrip() for line in
>     f if line.startswith("From "))
>
>     counts = counts.items()
>     counts.sort()
>     for hour, count in counts:
>          print hour, count
>
>     The most important part is getting items() and then sorting the
>     whole thing.
>



More information about the Python-list mailing list