Re: [Tutor] List vs. Set:

2018-02-25 Thread Steven D'Aprano
On Sat, Feb 24, 2018 at 08:00:58PM +, James Lundy wrote:

> Could this be a bug in my Pycharm 3 compiler I have had mixed 
> experiences running the code.

As a general rule, any time you get an unexpected error and think "Could 
this be a bug in the compiler?", the chances are almost always "No".

Which is more likely?

- tens or hundreds of thousands of people have used this compiler, and 
never noticed this bug? or

- the bug is in my code?

Even for experts, the chances are that the bug is in their code. The 
difference between the expert and the beginner is that the expert has a 
good shot at recognising that one-in-a-million time that it actually is 
an undiscovered bug in the compiler.

In the case of your code, I think the problem is this line:

> gooddata.append({singleday[2], singleday[3], singleday[4], 
> singleday[5]})

In particular, the part between the curly brackets (braces):

# curly brackets make a set
{ singleday[2], ... }

makes a set. My guess is that you were intending a list:

# square brackets make a list
[ singleday[2], ... ]

But there's a better way to fix this. You are grabbing four items out of 
a list, in order. Python has a short-cut for doing that:

# the long way
[singleday[2], singleday[3], singleday[4], singleday[5]]

# the short (and faster!) way
singleday[2:6]

Notice that the first index (2) is included, but the second index (6) is 
excluded. While it might seem confusing at first, this actually helps 
prevent "off by one" errors.

So the troublesome line becomes:

gooddata.append(singleday[2:6])

and, I believe, that ought to fix the bug. Possibly, or possibly not, 
to reveal any more bugs... *smiles*


Regards,


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


Re: [Tutor] List vs. Set:

2018-02-25 Thread Mark Lawrence

On 24/02/18 20:00, James Lundy wrote:

To whom it may concern. This code is from Dr. John Keyser.


Please arrange for him to go on a programming course :-)




gooddata = []

for singleday in datalist:
 if (singleday[0] == day) and (singleday[1] == month):


Yuck, unneeded brackets disease.


 gooddata.append({singleday[2], singleday[3], singleday[4], 
singleday[5]})


That looks like a set to me.



# Perform analysis
minsofar = 120
maxsofar = -100
numgooddates = 1
sumofmin = 0
sumofmax = 0

# so singleday in datalist is a list while singleday in gooddata is a 
set?


Seems like it.



for singleday in gooddata:

 sumofmin += singleday[1]
 sumofmax += singleday[2]
 if singleday[1] < minsofar:
 minsofar = singleday[1]
 if singleday[2] > maxsofar:
 maxsofar = singleday[2]

Could this be a bug in my Pycharm 3 compiler I have had mixed experiences 
running the code.


I very much doubt that.



An insertion of a space after for singleday in gooddata: line 54 caused the 
program to run as desired, once, since other runs cause the error


Really, I don't see how?




Traceback (most recent call last):

   File "C:/Users/Dad/PycharmProjects/TopDownDesign/WeatherDataSpecialDay.py", line 
56, in 

 sumofmin += singleday[1]

TypeError: 'set' object does not support indexing

persist.

I will attach the code and test file. Please allow me to thank you in advance 
for answering this query. I am new with Python and would appreciate any advice.


Put print calls into the code that you don't show above.  This should 
show what gets written to the datalist and it's type, ditto for the 
gooddata list.  I think you'll find that the code "works" when an 
invalid day and month gets input, yet produces the traceback when a 
valid day and month is input.




God Bless:

James Lundy
jalu...@computer.org



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


Re: [Tutor] List vs. Set:

2018-02-25 Thread Alan Gauld via Tutor
On 24/02/18 20:00, James Lundy wrote:
> To whom it may concern. This code is from Dr. John Keyser.

Since you don;t show us the complete program I will have
to make some assumptions...

> gooddata = []

This is degioned as a list by virtue of the []

> for singleday in datalist:
> if (singleday[0] == day) and (singleday[1] == month):
> gooddata.append({singleday[2], singleday[3], singleday[4], 
> singleday[5]})

This appends a set by virtue of the {}

The bracket type is what defines the data type.

> # Perform analysis
> minsofar = 120
> maxsofar = -100
> numgooddates = 1
> sumofmin = 0
> sumofmax = 0
> 
> # so singleday in datalist is a list while singleday in gooddata is a 
> set?

I don't know what singleday is since you don't show it's creation.
But singleday does not exist in gooddata. Instead you have created
a set that contains some elements from singleday. But those values
are copies that bear no relation to the original singleday elements.

> for singleday in gooddata:

This creates a new singleday object that is not related to the
original singleday. This one will be an instance of whatever
is in gooddata. In this case we know these are sets.

> sumofmin += singleday[1]

And you can't index a set. So you get an error.
If you want singleday to be a list you either need to insert
a list in the first loop or explicitly convert the set to
a list. But bear in mind that sets have no defined order
so you may not get the values out in the order you put
them in. And sets don;t allow duplicates so if two of
your original singleday values were identical one would
be thrown away.

I suspect you really wanted to use a list in the top
for loop:

if (singleday[0] == day) and (singleday[1] == month):
gooddata.append( [ singleday[2], singleday[3],
   singleday[4], singleday[5] ]
   )

Note however that the indexes will not be carried over, so in your
second loop singleday[1] refers to the old singleday[3].

If you want to retain the original indexes (and all the elements)
then just append singleday itself:

if (singleday[0] == day) and (singleday[1] == month):
gooddata.append( singleday )

> Could this be a bug in my Pycharm 3 compiler 

Nope, it is extremely unlikely that you will find a bug
in any compiler or interpreter(*). You should always assume
that the fault is in your code first and only consider
the compiler when all else has been eliminated.

(*)In 40 years of programming I've only once found
such a bug and it only manifested itself with a very
arcane - and slightly suspect - piece of code. Compilers
and interpreters tend to be very rigorously tested;
because they can be -  they have a very clearly defined
function.

> TypeError: 'set' object does not support indexing
> 
> persist.

Because you are creating a list of sets.
Change the set to a list and the problem will go away.

> I will attach the code and test file. 

The list server doesn't like attachments (although
text is usually OK) its better to paste things into
the message or provide a link to a web site)

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


[Tutor] List vs. Set:

2018-02-25 Thread James Lundy
To whom it may concern. This code is from Dr. John Keyser.


gooddata = []

for singleday in datalist:
if (singleday[0] == day) and (singleday[1] == month):
gooddata.append({singleday[2], singleday[3], singleday[4], 
singleday[5]})

# Perform analysis
minsofar = 120
maxsofar = -100
numgooddates = 1
sumofmin = 0
sumofmax = 0

# so singleday in datalist is a list while singleday in gooddata is a 
set?

for singleday in gooddata:

sumofmin += singleday[1]
sumofmax += singleday[2]
if singleday[1] < minsofar:
minsofar = singleday[1]
if singleday[2] > maxsofar:
maxsofar = singleday[2]

Could this be a bug in my Pycharm 3 compiler I have had mixed experiences 
running the code.

An insertion of a space after for singleday in gooddata: line 54 caused the 
program to run as desired, once, since other runs cause the error


Traceback (most recent call last):

  File "C:/Users/Dad/PycharmProjects/TopDownDesign/WeatherDataSpecialDay.py", 
line 56, in 

sumofmin += singleday[1]

TypeError: 'set' object does not support indexing

persist.

I will attach the code and test file. Please allow me to thank you in advance 
for answering this query. I am new with Python and would appreciate any advice.

God Bless:

James Lundy
jalu...@computer.org
## Read in Data 
# Open file
filename = input("Enter the name of the file: ")
infile = open(filename, 'r')
#print(infile.read())






# Read in data
datalist = []

for line in infile:
#get data form line
date, h, l, r = (line.split(','))
lowtemp = int(l)
hightemp = int(h)
rainfall = float(r)
m, d, y = date.split('/')
month = int(m)
day = int(d)
year = int(y)

#put data into list
datalist.append([day, month, year, lowtemp,hightemp, rainfall])



#Close file
infile.close()

## Analyze Data 
# Get Data of interest
month = int(input("For the date you care about, enter the month:"))
day = int(input("For the date you care about, enter the day: "))
# Fomd jostproca; data fpr date
gooddata = []

for singleday in datalist:
if (singleday[0] == day) and (singleday[1] == month):
gooddata.append({singleday[2], singleday[3], singleday[4], 
singleday[5]})

# Perform analysis
minsofar = 120
maxsofar = -100
numgooddates = 1
sumofmin = 0
sumofmax = 0

# so singleday in datalist is a list while singleday in gooddata is a 
set?

for singleday in gooddata:

sumofmin += singleday[1]
sumofmax += singleday[2]
if singleday[1] < minsofar:
minsofar = singleday[1]
if singleday[2] > maxsofar:
maxsofar = singleday[2]

avglow = sumofmin / numgooddates
avghigh = sumofmax / numgooddates


## Present results 
print ("There were", numgooddates,"days")
print ("The lowest", minsofar)
print ("The highet", maxsofar)
print ("The avaerage low has been", avglow)
print ("The average high", avghigh)


## Extra code for test #
# #greeting = "Howdy!How ar you today?I'm great!"
#lines = greeting.split('')
#for line in lines:
#print(line)

1/01/2000,79,37,0
1/02/2000,79,68,0
1/03/2000,73,60,0
1/04/2000,51,26,0
1/05/2000,57,19,0
1/06/2000,59,46,0.08
1/07/2000,58,35,2.08___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor