Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95263 > unrolled thread
| Started by | Ltc Hotspot <ltc.hotspot@gmail.com> |
|---|---|
| First post | 2015-08-11 17:01 -0700 |
| Last post | 2015-08-12 23:16 +0100 |
| Articles | 6 on this page of 46 — 10 participants |
Back to article view | Back to comp.lang.python
AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-11 17:01 -0700
Re: AttributeError leo kirotawa <kirotawa@gmail.com> - 2015-08-11 21:16 -0300
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 01:26 +0100
Re: AttributeError Chris Angelico <rosuav@gmail.com> - 2015-08-12 10:49 +1000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 01:58 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-11 22:03 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 16:12 +0100
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 15:50 +0000
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 09:29 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 17:45 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 09:57 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 18:24 +0100
RE: AttributeError "Clayton Kirkwood" <crk@godblessthe.us> - 2015-08-12 10:31 -0700
Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 10:37 -0700
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 11:35 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 19:59 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 12:05 -0700
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 20:38 +0000
RE: AttributeError "Clayton Kirkwood" <crk@godblessthe.us> - 2015-08-12 14:15 -0700
Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 14:32 -0700
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 22:45 +0100
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 22:51 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 14:04 -0700
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-13 10:37 +0100
Re: AttributeError Ben Finney <ben+python@benfinney.id.au> - 2015-08-13 21:08 +1000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 21:04 +0100
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 21:16 +0000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 23:35 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 16:05 -0700
Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 16:15 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-13 00:16 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 16:46 -0700
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-13 09:15 +0000
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-13 02:41 -0700
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-13 11:06 +0000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-13 00:56 +0100
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 20:28 +0000
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 21:58 +0100
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 03:46 +0100
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 04:32 +0100
Re: AttributeError Chris Angelico <rosuav@gmail.com> - 2015-08-12 13:49 +1000
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 11:36 +0000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 16:09 +0100
Re: AttributeError Gene Heskett <gheskett@wdtv.com> - 2015-08-12 17:25 -0400
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 15:02 -0700
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 23:16 +0100
Page 3 of 3 — ← Prev page 1 2 [3]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-08-12 13:49 +1000 |
| Message-ID | <mailman.98.1439351399.3627.python-list@python.org> |
| In reply to | #95263 |
On Wed, Aug 12, 2015 at 1:05 PM, Ltc Hotspot <ltc.hotspot@gmail.com> wrote: > > Check the code and the visualize execution of the code, available at > http://tinyurl.com/p8tgd5h > > message reads: NameError: name 'collections' is not defined I've no idea why you made this Frankenstein monster out of your original code and my suggestion, because there's no way that's going to work. Go back to the people who've helped you on the python-tutor list, and *do what they have requested* in terms of showing code, not top-posting, and so on. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-08-12 11:36 +0000 |
| Message-ID | <mqfb3k$8np$1@dont-email.me> |
| In reply to | #95263 |
On Tue, 11 Aug 2015 17:01:24 -0700, Ltc Hotspot wrote:
> What is the list equivalent to line 12: ncount.sort(reverse=True)
>
> count = dict()
> fname = raw_input("Enter file name: ")#
> handle = open (fname, 'r')#
> for line in handle:
> if line.startswith("From "):
> address = line.split()[5]
> line = line.rstrip()
> count[address] = count.get(address, 0) + 1
At this point, count seems to be a dictionary of address: count of lines
> for key,val in count.items():
> ncount = (key,val) ncount.sort(reverse=True) print key,val
ncount is a single key-value pair. Why are you trying to sort ncount?
Do you want results ordered by count?
First, change your dictionary into a list of tuples:
ncount = [(a,c) for a,c in count.items()]
Then sort ncount on the second field of the tuple:
ncount.sort(key = lambda x: x[1], reverse=True)
print ncount
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-08-12 16:09 +0100 |
| Message-ID | <mailman.108.1439392156.3627.python-list@python.org> |
| In reply to | #95263 |
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@gmail.com
> <mailto:rosuav@gmail.com>> wrote:
>
> On Wed, Aug 12, 2015 at 10:43 AM, Ltc Hotspot <ltc.hotspot@gmail.com
> <mailto:ltc.hotspot@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@uct.ac.za <mailto:stephen.marquard@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.
>
[toc] | [prev] | [next] | [standalone]
| From | Gene Heskett <gheskett@wdtv.com> |
|---|---|
| Date | 2015-08-12 17:25 -0400 |
| Message-ID | <mailman.139.1439414731.3627.python-list@python.org> |
| In reply to | #95263 |
On Wednesday 12 August 2015 15:05:37 Ltc Hotspot wrote:
> >Have a look at assignment_10_2_v_06.py.
>
> What should I look at assignment_10_2_v_06.py.:
>
>
> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
> From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
> """.split("\n")
> # Snippet file data: mbox-short.txt
>
> count = dict()
> #fname = raw_input("Enter file name: ")# Add Snippet file
> #handle = open (fname, 'r')# Add Snippet file
> for line in handle:
> if line.startswith("From "):
> time = line.split() # Sort time
>
> hours = line.split(":")[5] # Sort hours
> line = line.rstrip()
>
> count[hours] = count.get(hours, 0) + 1 # counter
>
> lst = [(val,key) for key,val in count.items()]
>
> print key, val
>
> On Wed, Aug 12, 2015 at 11:59 AM, MRAB <python@mrabarnett.plus.com>
wrote:
> > On 2015-08-12 19:35, Ltc Hotspot wrote:
> >> Emile
> >>
> >> How do I define time in the revised code ?
> >
> > Have a look at assignment_10_2_v_06.py.
> >
> >> -------------------------------------------------------------------
> >>-------- Traceback Message reads:
> >> In [66]: %run assignment_10_2_v_07
> >>
> >> NameError
> >> Traceback (most recent call last)
> >> C:\Users\vm\Desktop\apps\docs\Python\week_10\assignment_10_2_v_07.p
> >>y in <module>
> >> ()
> >> 9 for line in handle:
> >> 10 if line.startswith("From "):
> >> ---> 11 for key, val in time.split()[5]:
> >> 12 for key, val in hours.split(':')[0]:
> >> 13 count[hours] = count.get(hours, 0) + 1 #
> >> counter
> >>
> >> NameError: name 'time' is not defined
> >>
> >> -------------------------------------------------------------------
> >>--------
> >>
> >>
> >> In [67]: print time
> >> 0
> >>
> >> -------------------------------------------------------------------
> >>--------
> >>
> >> Revised Code reads:
> >> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16
> >> 2008
> >>
> >>> From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
> >>
> >> """.split("\n")
> >> # Snippet file data: mbox-short.txt
> >>
> >> count = dict()
> >> #fname = raw_input("Enter file name: ")# insert snippet file
> >> #handle = open (fname, 'r')# insert snippet file
> >> for line in handle:
> >> if line.startswith("From "):
> >> for key, val in time.split()[5]:
> >> for key, val in hours.split(':')[0]:
> >> count[hours] = count.get(hours, 0) + 1 # counter
> >>
> >> lst = [(val,key) for key,val in count.items()]
> >> lst.sort(reverse=True)
> >>
> >> for key, val in lst[:12] :
> >> print key, val
> >>
> >>
> >> -------------------------------------------------------------------
> >>-------- Regards,
> >> Hal
> >
> > I'm baffled as to why you now have a nested 'for' loop 3 levels
> > deep!
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
After following this thread to late afternoon today, I would say that
Denis's response was a bit strong and less than helpfull because the
perceived insult drowns out the true meaning of his remark.
OTOH, _you_ are putting zero effort into understanding the error,
something the others have encouraged, apparently several times now on at
least 2 other venues.
This IS how one learns best, by seeing an error and taking an action that
aleviates the error. Handing the answer to you on a clean plate may fix
the error, but your chances of understanding why it was fixed are
perhaps 10% as good than the understanding in the future would be if you
followed normal debugging proceedures and fixed it yourself.
If you aren't willing to put any effort into it yourself, then perhaps
Denis does have the right idea. Find another line of endeavor that
_will_ pay the bills.
Cheers, Gene Heskett
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>
[toc] | [prev] | [next] | [standalone]
| From | Ltc Hotspot <ltc.hotspot@gmail.com> |
|---|---|
| Date | 2015-08-12 15:02 -0700 |
| Message-ID | <mailman.143.1439416953.3627.python-list@python.org> |
| In reply to | #95263 |
The problem here is that these are home work assignment and to code an
else word assignment would be in contradiction to the academic
curriculum.
Specifically, I want to deploy a quick and simply dic solution:
Revised code:
c = [0 for i in range(24)]
filename = raw_input("Enter file name: ")
f = open(filename,'r')
for l in f:
h = int(l.strip().split()[X].split(':')[Y])
c[h] = c[h] + 1
f.close()
for i in range(24):
print '{:02d} {}'.format(i, c[i])
---------------------------------------------------------------------------
NameError
Traceback (most recent call last)
C:\Users\vm\Desktop\apps\docs\Python\week_10\assignment_10_2_v_10.py in <module>
()
5 f = open(filename,'r')
6 for l in f:
----> 7 h = int(l.strip().split()[X].split(':')[Y])
8 c[h] = c[h] + 1
9 f.close()
NameError: name 'X' is not defined
In [74]: print X
---------------------------------------------------------------------------
Print X:
NameError
Traceback (most recent call last)
<ipython-input-74-7510f249b93c> in <module>()
----> 1 print X
NameError: name 'X' is not defined
In [75]:
On Wed, Aug 12, 2015 at 2:25 PM, Gene Heskett <gheskett@wdtv.com> wrote:
> On Wednesday 12 August 2015 15:05:37 Ltc Hotspot wrote:
>
>> >Have a look at assignment_10_2_v_06.py.
>>
>> What should I look at assignment_10_2_v_06.py.:
>>
>>
>> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
>> From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
>> """.split("\n")
>> # Snippet file data: mbox-short.txt
>>
>> count = dict()
>> #fname = raw_input("Enter file name: ")# Add Snippet file
>> #handle = open (fname, 'r')# Add Snippet file
>> for line in handle:
>> if line.startswith("From "):
>> time = line.split() # Sort time
>>
>> hours = line.split(":")[5] # Sort hours
>> line = line.rstrip()
>>
>> count[hours] = count.get(hours, 0) + 1 # counter
>>
>> lst = [(val,key) for key,val in count.items()]
>>
>> print key, val
>>
>> On Wed, Aug 12, 2015 at 11:59 AM, MRAB <python@mrabarnett.plus.com>
> wrote:
>> > On 2015-08-12 19:35, Ltc Hotspot wrote:
>> >> Emile
>> >>
>> >> How do I define time in the revised code ?
>> >
>> > Have a look at assignment_10_2_v_06.py.
>> >
>> >> -------------------------------------------------------------------
>> >>-------- Traceback Message reads:
>> >> In [66]: %run assignment_10_2_v_07
>> >>
>> >> NameError
>> >> Traceback (most recent call last)
>> >> C:\Users\vm\Desktop\apps\docs\Python\week_10\assignment_10_2_v_07.p
>> >>y in <module>
>> >> ()
>> >> 9 for line in handle:
>> >> 10 if line.startswith("From "):
>> >> ---> 11 for key, val in time.split()[5]:
>> >> 12 for key, val in hours.split(':')[0]:
>> >> 13 count[hours] = count.get(hours, 0) + 1 #
>> >> counter
>> >>
>> >> NameError: name 'time' is not defined
>> >>
>> >> -------------------------------------------------------------------
>> >>--------
>> >>
>> >>
>> >> In [67]: print time
>> >> 0
>> >>
>> >> -------------------------------------------------------------------
>> >>--------
>> >>
>> >> Revised Code reads:
>> >> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16
>> >> 2008
>> >>
>> >>> From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
>> >>
>> >> """.split("\n")
>> >> # Snippet file data: mbox-short.txt
>> >>
>> >> count = dict()
>> >> #fname = raw_input("Enter file name: ")# insert snippet file
>> >> #handle = open (fname, 'r')# insert snippet file
>> >> for line in handle:
>> >> if line.startswith("From "):
>> >> for key, val in time.split()[5]:
>> >> for key, val in hours.split(':')[0]:
>> >> count[hours] = count.get(hours, 0) + 1 # counter
>> >>
>> >> lst = [(val,key) for key,val in count.items()]
>> >> lst.sort(reverse=True)
>> >>
>> >> for key, val in lst[:12] :
>> >> print key, val
>> >>
>> >>
>> >> -------------------------------------------------------------------
>> >>-------- Regards,
>> >> Hal
>> >
>> > I'm baffled as to why you now have a nested 'for' loop 3 levels
>> > deep!
>> >
>> > --
>> > https://mail.python.org/mailman/listinfo/python-list
>
> After following this thread to late afternoon today, I would say that
> Denis's response was a bit strong and less than helpfull because the
> perceived insult drowns out the true meaning of his remark.
>
> OTOH, _you_ are putting zero effort into understanding the error,
> something the others have encouraged, apparently several times now on at
> least 2 other venues.
>
> This IS how one learns best, by seeing an error and taking an action that
> aleviates the error. Handing the answer to you on a clean plate may fix
> the error, but your chances of understanding why it was fixed are
> perhaps 10% as good than the understanding in the future would be if you
> followed normal debugging proceedures and fixed it yourself.
>
> If you aren't willing to put any effort into it yourself, then perhaps
> Denis does have the right idea. Find another line of endeavor that
> _will_ pay the bills.
>
> Cheers, Gene Heskett
> --
> "There are four boxes to be used in defense of liberty:
> soap, ballot, jury, and ammo. Please use in that order."
> -Ed Howdershelt (Author)
> Genes Web page <http://geneslinuxbox.net:6309/gene>
> --
> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-08-12 23:16 +0100 |
| Message-ID | <mailman.144.1439417812.3627.python-list@python.org> |
| In reply to | #95263 |
On 12/08/2015 23:02, Ltc Hotspot wrote: FOR GOD'S SAKE WILL YOU PLEASE STOP TOP POSTING AND TRIM DOWN YOUR REPLIES. WHAT GRADE DO YOU EXPECT, A Z-? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.python
csiph-web