[Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread D Wyatt
I just read in a book a little while ago that ** trumps a negative
sign?  I am struggling with the audacity of that as -1 is negative 1,
NOT minus 1.  How can an arithmetic operation trump an attribute of a
negative integer?  It truly makes no sense to me.  Thank you for any
enlightenment you can provide.

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 3**2
9
>>> (-3)**2
9
>>> -3**2
-9
>>>

-- 
Deb Wyatt in WA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot










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

2015-07-31 Thread ltc.hotspot



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

2015-07-31 Thread Alan Gauld

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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Alan Gauld

On 31/07/15 01:58, D Wyatt wrote:

I just read in a book a little while ago that ** trumps a negative
sign?  I am struggling with the audacity of that as -1 is negative 1,
NOT minus 1.  How can an arithmetic operation trump an attribute of a
negative integer?


Because Python is a programming language with its own rules
created by its designer. It doesn't need to follow the rules
of math as you understand them.

The decision may have been made because it made parsing
constructs like this easier:

5-2

Is that 2 adjacent numbers 5 and -2? or is it an infix subtraction?
Its easy for us to decide but harder for a computer reading the text.
As the expressions get more complex there are more and more potentially 
ambiguous combinations so the language designer may decide to make the 
language more consistent by defining a set of precedence rules. In

this case that ** is higher than -.

He is quite within his rights to do that. It's his language after all.
Some languages solve these problems by not permitting infix notation,
so in Lisp for example

(3 - 5)

is illegal, you need to do

(- 3 5)

It looks odd to us but that's not the point, its how the language works.
You learn to get used to it. Most languages have some idiosyncrasies 
like this.


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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Todd
On Fri, Jul 31, 2015 at 2:58 AM, D Wyatt  wrote:

> I just read in a book a little while ago that ** trumps a negative
> sign?  I am struggling with the audacity of that as -1 is negative 1,
> NOT minus 1.  How can an arithmetic operation trump an attribute of a
> negative integer?  It truly makes no sense to me.  Thank you for any
> enlightenment you can provide.
>
> Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32
> bit (In
> tel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> 3**2
> 9
> >>> (-3)**2
> 9
> >>> -3**2
> -9
> >>>
>
>
It is a matter of operator precedence.  Certain operators are carried out
before others.  See here:
https://docs.python.org/3/reference/expressions.html#operator-precedence

Negation has lower precedence than exponentiation.  That means that the
exponentiation is carried out first, and negation is carried out second.
So "-3**2" is equivalent to "-(3**2)".

This matches the precedence rules for written mathematics, where negation
has a lower precedence than exponentiation as well.  So python is doing the
correct thing here mathematically.  See, for example,
http://mathforum.org/library/drmath/view/53194.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Jose Amoreira

Hello!
On 07/31/2015 01:58 AM, D Wyatt wrote:

I just read in a book a little while ago that ** trumps a negative
sign?  I am struggling with the audacity of that as -1 is negative 1,
NOT minus 1.


I'm not sure about what you mean by "trumps", but the square of negative 
one is positive one (negative times negative gives positive).



How can an arithmetic operation trump an attribute of a
negative integer?  It truly makes no sense to me.  Thank you for any
enlightenment you can provide.

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

3**2

9

(-3)**2

9

-3**2

-9






Given the precedence rules already mentioned by Alan and Todd, the 
results of the operations you showed us are exactly as expected. You'll 
get the same results if you try with a pocket calculator or using any 
other programming language or scientific package. Or MS-excel.


HTH,
Ze
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Válas Péter
2015-07-31 2:58 GMT+02:00 D Wyatt :

>
> >>> 3**2
> 9
> >>> (-3)**2
> 9
> >>> -3**2
> -9
> >>>
>
>
Try to get any other result for these operations in a primary school paper,
and then have a look at your marks...
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Alan Gauld

On 31/07/15 10:55, Jose Amoreira wrote:


Given the precedence rules already mentioned by Alan and Todd, the
results of the operations you showed us are exactly as expected. You'll
get the same results if you try with a pocket calculator or using any
other programming language or scientific package.


The point I was making is that you may NOT get the same results in any 
other language. Each language designer is free to define his/her own 
precedence rules. Most try to follow the rules of math, but some do not 
(like Lisp using prefix notation rather than infix). Several languages 
evaluate purely left to right, at least 1 goes from right to left.


In programming you have to learn the  language and not expect it to 
conform to any preconceived ideas of how it *should* work.


--
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


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Jose Amoreira

On 07/31/2015 11:36 AM, Alan Gauld wrote:

On 31/07/15 10:55, Jose Amoreira wrote:


Given the precedence rules already mentioned by Alan and Todd, the
results of the operations you showed us are exactly as expected. You'll
get the same results if you try with a pocket calculator or using any
other programming language or scientific package.


The point I was making is that you may NOT get the same results in any
other language. Each language designer is free to define his/her own
precedence rules. Most try to follow the rules of math, but some do not
(like Lisp using prefix notation rather than infix). Several languages
evaluate purely left to right, at least 1 goes from right to left.

In programming you have to learn the  language and not expect it to
conform to any preconceived ideas of how it *should* work.



Yes, you are right, Alan. That expectation is often the source of much 
frustration when learning a new language.
But, for this particular application, this particular language even 
conforms to those general preconceived ideas... And I'm glad it does!


:)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Steven D'Aprano
On Thu, Jul 30, 2015 at 05:58:27PM -0700, D Wyatt wrote:
> I just read in a book a little while ago that ** trumps a negative
> sign?  

Correct. Exponentiation has higher priority than subtraction, addition, 
multiplication and division:

2+3**2 => 11 not 25

10-3**2 => 1 not 49

2*3**2 => 18 not 36

100/5**2 => 4 not 400

As you have discovered, it also has higher priority than unary minus so 
that:

-3**2 => -(3**2) => -9 NOT (-3)**2 => 9

Mathematically, this is perfectly acceptable, and what we would 
normally expect. In algebra, if we write:

-x² 

we normally mean the negative of (x squared), not (negative x) squared, 
which would be just x². So Python here agrees with standard mathematical 
notation.


> I am struggling with the audacity of that as -1 is negative 1,
> NOT minus 1.  How can an arithmetic operation trump an attribute of a
> negative integer?  It truly makes no sense to me.  Thank you for any
> enlightenment you can provide.

Speaking as a maths tutor with about 20 years experience, and a B.Sc. 
with a major in mathematics, I'm not sure I understand what you are 
getting at. There is no mathematical difference between the inherent 
negativeness of -1 and the arithmetic operation - 1 (unary minus 
operator followed by 1).

Whichever way you treat it, we have to agree what it means. For example, 
2x means 2 multiplied by x; but 23 doesn't mean 2 multiplied by 3. It 
could if we wanted it to, but that would be inconvenient. Mathematicians 
could define -3² as (-3)² = 9 if they wanted to, but generally they 
don't, although there are exceptions. Consequently such expressions are 
ambiguous and are best avoided. Although -x² never means -x squared, 
it always means minus (x squared).

Python removes the ambiguity and has exponentiation always take priority 
over other arithmetic operators, regardless of whether you consider - a 
unary or binary operator, or an inherent part of the integer.

Although, for the record, the standard Python interpreter does NOT parse 
an expression like -123 as "negative 123", but as "unary minus 123". In 
practice this makes zero difference to the code.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot




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



[Tutor] Python application with same name as its corresponding project

2015-07-31 Thread Anthony DuPont
I am trying to setup my python application in a more standard way. In my
research, the general recommendation seems to be that if my application is
called projectgendocs, here is an acceptable structure:

ProjectParent
|-- bin/
|  |-- projectgendocs.py
|
|-- projectgendocs
|  |-- unittest
|  |  |-- __init__.py
|  |  |-- test_main.py
|  |
|  |-- __init__.py
|  |-- main.py
|
|-- setup.py
|-- README


It is my understanding that ProjectParent would be added to the PYTHONPATH
so the projectgendocs project could be discovered for importing. The
problem I am encountering is that when the bin/projectgendocs.py is run, it
breaks the imports in the ProjectParent/projectgendocs files that have

import projectgendocs.somefile.py

I discovered this is due to the fact that the bin/projectgendocs is
discovered in the search for a package called projectgendocs because the
bin directory is added to the search path. I verified this by renaming the
bin/projectgendocs.py file to something different. So, my question is this:
How can a python application (the file installed in the bin folder) have
the same name as its corresponding python project and not break the
absolute imports? Or is the only way to do this is by using relative
imports?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-07-31 Thread Alan Gauld

On 31/07/15 15:39, 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]
 addresses = set()


Notice I said you had to create and initialize the set
*above* the loop.
Here you are creating a new set every time round the
loop and throwing away the old one.


 addresses.add(line4)
 count = count + 1
 print addresses


And notice I said to move the print statement
to *after* the loop so as to print the complete set,
not just the current status.


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'])


Thats correct because you create a new set each time
and add precisely one element to it before throwing
it away and starting over next time round.


Question no. 1: is there a build in function for set that parses the data for 
duplicates.


No because thats what a set does. it is a collection of
unique items. It will not allow duplicates.

Your problem is you create a new set of one item for
every line. So you have multiple sets with the same
data in them.


  Question no. 2: Why is there not a building function for append?


add() is the equivalent of append for a set.
If you try to add() a value that already exists it
will be ignored.


Question no. 3: If all else fails, i.e., append & set,  my only option is the 
slice the data set?


No there are lots of other options but none of them are necessary 
because a set is a collection of unique values. You just need to

use it properly. Read my instructions again, carefully:


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.




--
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


Re: [Tutor] String Attribute

2015-07-31 Thread Martin A. Brown


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?


Question no. 3: If all else fails, i.e., append & set, my only 
option is the slice the data set?


I do not understand these two questions.

Good luck.

-Martin

P.S. By the way, Alan Gauld has also responded to your message, with
  a differently-phrased answer, but, fundamentally, he and I are
  saying the same thing.  Think about where you are initializing
  your variables, and know that 'addresses = set()' in the middle
  of the code is re-initializing the variable and throwing away
  anything that was there before..

--
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


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Válas Péter
2015-07-31 19:23 GMT+02:00 D Wyatt :

> > Try to get any other result for these operations in a primary school
> paper,
> > and then have a look at your marks...
> >
>
> Condescending and unhelpful response.  Why did you bother?
>
>
I tell you another way. These ARE the arithmetically correct results. Why
do you expect Python give wrong ones?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python application with same name as its corresponding project

2015-07-31 Thread Chris Warrick
On 31 July 2015 at 17:13, Anthony DuPont  wrote:
> I am trying to setup my python application in a more standard way. In my
> research, the general recommendation seems to be that if my application is
> called projectgendocs, here is an acceptable structure:
>
> ProjectParent
> |-- bin/
> |  |-- projectgendocs.py
> |
> |-- projectgendocs
> |  |-- unittest
> |  |  |-- __init__.py
> |  |  |-- test_main.py
> |  |
> |  |-- __init__.py
> |  |-- main.py
> |
> |-- setup.py
> |-- README
>
>
> It is my understanding that ProjectParent would be added to the PYTHONPATH
> so the projectgendocs project could be discovered for importing. The
> problem I am encountering is that when the bin/projectgendocs.py is run, it
> breaks the imports in the ProjectParent/projectgendocs files that have
>
> import projectgendocs.somefile.py
>
> I discovered this is due to the fact that the bin/projectgendocs is
> discovered in the search for a package called projectgendocs because the
> bin directory is added to the search path. I verified this by renaming the
> bin/projectgendocs.py file to something different. So, my question is this:
> How can a python application (the file installed in the bin folder) have
> the same name as its corresponding python project and not break the
> absolute imports? Or is the only way to do this is by using relative
> imports?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

Don’t create custom bin/ scripts, use setuptools entry points. I
described it on my blog:

https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/

-- 
Chris Warrick 
PGP: 5EAAEA16
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Alan Gauld

On 31/07/15 18:08, D Wyatt wrote:

> It looks odd to us but that's not the point, its how the language works.
> You learn to get used to it. Most languages have some idiosyncrasies like
> this.

Yes, I understand that the creator of the language can make it work
however he wants, but I was really hoping for a logical answer.  Just
because 'that's the way it is' kind of sucks and will make it more
difficult to remember.

Many languages do their own thing because it makes the compiler go faster,
or makes the code produced more reliable/consistent. That's often more
important to a language designer than making it intuitive to the reader.

In fact, some languages deliberately use a different set of 
rules/notation because
the language designer believes that his/her way is superior to the 
traditional

notation and uses the language to test those ideas.

Now, as others have pointed out, Python does in fact follow traditional 
math
in most things, so is usually intuitively correct, but you should never 
assume
that of any programming language. The designers are often reaching for 
different

targets than the eventual user. And very often we the users don't know what
the designer's aims or priorities were. (For example Forth was designed 
to fit

into the very small amount of memory left over on an astronomical telescope
control system, so is very, very terse, and uses many "illogical" code 
layouts.

Everything was sacrificed to save space.)

Of course as users we get to decide whether the designers choices are
acceptable to us or whether we will adopt another language. Many
experimental languages full of good, powerful programming ideas never
made it into common use precisely because they chose some weird syntax
or layout convention that was just too far out for programmers to accept.

--
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


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Steven D'Aprano
Hi Deb,

On Fri, Jul 31, 2015 at 10:48:57AM -0700, D Wyatt wrote:

> I have never really thought about any of this before, but many of you
> have responded like this is so obvious.  That is not helpful.  I'm
> looking at a negative number as being an object that is one less than
> zero, and the unary sign being a part of that object, glued to it.
> Why is that so hard to understand?

Like many things, the behaviour here is obvious in hindsight. And you 
are right, sometimes people forget that hindsight is 20:20 but foresight 
is not. If I said anything that gave you the impression that your 
question was a dumb question, sorry, that wasn't my intention.

In this case, your expectation that -3**2 should be treated as -3 as a 
single value, raised to the power of 2, is a perfectly reasonable 
expectation. I daresay that there are programming languages where that 
actually is the case, and if you search the Internet, you'll find lots 
of people making the same assumption as you.


Regards,


Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread D Wyatt

>
> He is quite within his rights to do that. It's his language after all.
> Some languages solve these problems by not permitting infix notation,
> so in Lisp for example
>
> (3 - 5)
>
> is illegal, you need to do
>
> (- 3 5)
>
> It looks odd to us but that's not the point, its how the language works.
> You learn to get used to it. Most languages have some idiosyncrasies like
> this.
>
> 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
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

Yes, I understand that the creator of the language can make it work
however he wants, but I was really hoping for a logical answer.  Just
because 'that's the way it is' kind of sucks and will make it more
difficult to remember.

-- 
Deb Wyatt in WA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread D Wyatt

>
> Mathematically, this is perfectly acceptable, and what we would
> normally expect. In algebra, if we write:
>
> -x²
>
> we normally mean the negative of (x squared), not (negative x) squared,
> which would be just x². So Python here agrees with standard mathematical
> notation.
>
>

> Speaking as a maths tutor with about 20 years experience, and a B.Sc.
> with a major in mathematics, I'm not sure I understand what you are
> getting at. There is no mathematical difference between the inherent
> negativeness of -1 and the arithmetic operation - 1 (unary minus
> operator followed by 1).
>
> Whichever way you treat it, we have to agree what it means. For example,
> 2x means 2 multiplied by x; but 23 doesn't mean 2 multiplied by 3. It
> could if we wanted it to, but that would be inconvenient. Mathematicians
> could define -3² as (-3)² = 9 if they wanted to, but generally they
> don't, although there are exceptions. Consequently such expressions are
> ambiguous and are best avoided. Although -x² never means -x squared,
> it always means minus (x squared).

>
>
> --
> Steve

I have never really thought about any of this before, but many of you
have responded like this is so obvious.  That is not helpful.  I'm
looking at a negative number as being an object that is one less than
zero, and the unary sign being a part of that object, glued to it.
Why is that so hard to understand?  So, mathematically, it works the
way python works.  I never realized that, though I've always been good
at math.  It's just not something that has ever explicitly come up
anywhere until I read it in the Python book.

The whole purpose of this email group is to share knowledge and help
each other, but when you forget that what seems obvious to you is not
obvious to everybody, you lose the ability to be as helpful as you
could be.  I'm sorry my question turned out to be more about math than
Python.  I had no idea.

This semi-rant is NOT directed at you, personally, Steve.  I would
like to thank almost everybody for their responses.  I've learned a
few things.  But I'll keep asking questions anyway, even if some of
your responses imply they're stupid questions.  The only stupid
question is the question you don't ask.
-- 
Deb Wyatt in WA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread D Wyatt

>
> This matches the precedence rules for written mathematics, where negation
> has a lower precedence than exponentiation as well.  So python is doing the
> correct thing here mathematically.  See, for example,
> http://mathforum.org/library/drmath/view/53194.html
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


That is just so counterintuitive, and I've never run into this in any
mathematics I have taken.  Now I'm going to have to research this
further, from a mathematics standpoint.
-- 
Deb Wyatt in WA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python application with same name as its corresponding project

2015-07-31 Thread Japhy Bartlett
Like Chris mentions, usually you don't write your own stuff in /bin/.

To make what you've written work anyhow, you can run them from inside
/ProjectParent/, not from inside /ProjectParent/bin/.

eg, `python bin/projectgendocs.py`

On Fri, Jul 31, 2015 at 1:16 PM, Chris Warrick  wrote:

> On 31 July 2015 at 17:13, Anthony DuPont  wrote:
> > I am trying to setup my python application in a more standard way. In my
> > research, the general recommendation seems to be that if my application
> is
> > called projectgendocs, here is an acceptable structure:
> >
> > ProjectParent
> > |-- bin/
> > |  |-- projectgendocs.py
> > |
> > |-- projectgendocs
> > |  |-- unittest
> > |  |  |-- __init__.py
> > |  |  |-- test_main.py
> > |  |
> > |  |-- __init__.py
> > |  |-- main.py
> > |
> > |-- setup.py
> > |-- README
> >
> >
> > It is my understanding that ProjectParent would be added to the
> PYTHONPATH
> > so the projectgendocs project could be discovered for importing. The
> > problem I am encountering is that when the bin/projectgendocs.py is run,
> it
> > breaks the imports in the ProjectParent/projectgendocs files that have
> >
> > import projectgendocs.somefile.py
> >
> > I discovered this is due to the fact that the bin/projectgendocs is
> > discovered in the search for a package called projectgendocs because the
> > bin directory is added to the search path. I verified this by renaming
> the
> > bin/projectgendocs.py file to something different. So, my question is
> this:
> > How can a python application (the file installed in the bin folder) have
> > the same name as its corresponding python project and not break the
> > absolute imports? Or is the only way to do this is by using relative
> > imports?
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
>
> Don’t create custom bin/ scripts, use setuptools entry points. I
> described it on my blog:
>
>
> https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/
>
> --
> Chris Warrick 
> PGP: 5EAAEA16
> ___
> 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] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Válas Péter
2015-07-31 20:51 GMT+02:00 Alan Gauld :

>
> (For example Forth was designed to fit
> into the very small amount of memory left over on an astronomical telescope
> control system, so is very, very terse, and uses many "illogical" code
> layouts.
> Everything was sacrificed to save space.)
>

Thank you for this interesting story, I never knew this. You mentioned such
interesting languages as Lisp and Forth. While you are right that operator
precedence may vary from language to language (just a few weeks ago PHP
schocked me with the different precedence of and/or and "C-style" &&/|| and
it took me quite a time to find the error), I think we may say that all the
mainstream languages of present times leave the precedence of ARITHMETICAL
operators intact among each other. And this follows the rules of elementary
math.

While thinking that minus is glued to the following number has its own
logic and is not hard to understand this logic, it's not the rule of
mathematics. That's why math has its worldwide accepted rules, and that's
why these are taught in schools whereever we go on the globe. These rules
are also based on logics, of course. And because prorgramming is a branch
of applied mathematics, it is really-really worth to keep the rules of math
-- I think where they are broken, it is an exception and not the normal
workflow.

While all we should read the famous manual concerning the precedence when
we learn a new language, we may trust that * will always preceed +, ** will
preceed any other arithmetical operator, and AND will preceed OR -- unless
we deal  with some special/obscure/historical language. Python is not an
exception.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot
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] a puzzle about -3**2 vs (-3)**2

2015-07-31 Thread Alan Gauld

On 31/07/15 18:48, D Wyatt wrote:


I have never really thought about any of this before, but many of you
have responded like this is so obvious.  That is not helpful.


That's a fair point.

In our defence I guess that many of the active "tutors" on the list are 
professional programmers. And programming originated, and is often still 
taught, as a branch of math. Which means we tend to have a background in 
math and therefore tend to see a lot of math stuff

as "obvious" when, of course, it may not be to someone without
that background.


... when you forget that what seems obvious to you is not
obvious to everybody, you lose the ability to be as helpful as you
could be.


Again a very valid point. We try to keep our answers general and
not assume too much background. But sometimes you forget how
much you think is 'obvious' is really just
"learned a long time ago"...


The only stupid question is the question you don't ask.


And that too is true. Thanks for asking it, the discussion is
always useful and, hopefully, you will not be the only one to
benefit.

--
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


Re: [Tutor] String Attribute

2015-07-31 Thread Emile van Sebille

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


Re: [Tutor] String Attribute

2015-07-31 Thread Alan Gauld

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.



  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


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.


→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

That will show you what the data looks like and you can tell
whether line3[1] makes any kind of sense.


--
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


Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot
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

2015-07-31 Thread Mark Lawrence

On 31/07/2015 19:57, ltc.hots...@gmail.com wrote:

I believe that this is the third time that you've been asked to do 
something about the amount of whitespace that you're sending to this list.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor