[Tutor] List vs. Set:

Alan Gauld alan.gauld at yahoo.co.uk
Sun Feb 25 04:26:27 EST 2018


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




More information about the Tutor mailing list