0

I created a class:

class aTest:
    name = ""
    twoGroups = -1
    continuous = -1
    parametric = -1
    covariates = -1
    paired = -1

    def __init__(self, name, twoGroups, continuous, parametric, covariates, paired):
        self.name = name
        self.twoGroups = twoGroups
        self.continuous = continuous
        self.parametric = parametric
        self.covariates = covariates
        self.paired = paired

And then created a list of class instances:

testList = []
testList.append(aTest("independent samples t-test", 1, 1, 1, 0, 0))
testList.append(aTest("dependent samples t-test", 1, 1, 1, 0, 1))
testList.append(aTest("Mann-Whitney U-test", 1, 1, 0, 0, 0))
testList.append(aTest("Wilcoxson matched pairs test", 1, 1, 0, 0, 1))
testList.append(aTest("one-way independent ANOVA", 0, 1, 1, 0, 0))
testList.append(aTest("Kruskall-Wallis test", 0, 1, 0, 0, 0))
testList.append(aTest("one-way repeated measures ANOVA", 0, 1, 1, 0, 1))
testList.append(aTest("Friedman's ANOVA", 0, 1, 0, 0, 1))
testList.append(aTest("ANCOVA", 0, 1, 1, 1, 0))

Then, finally, I tried to remove all the items in the list where the attribute parametric was equal to 0:

print("Now, remove the nonparametric tests...")
for test in testList:
    if test.parametric == 0:
        testList.remove(test)

print("...and print what's left, with confirmation the attribute was 0")
for test in testList:
    print(test.name+", "+str(test.parametric))

I get this result in the console:

> Now, remove the nonparametric tests... 
> ...and print what's left, with confirmation the attribute was 0
> independent samples t-test, 1
> dependent samples t-test, 1
> Wilcoxson matched pairs test, 0
> one-way independent ANOVA, 1
> one-way repeated measures ANOVA, 1
> ANCOVA, 1

What's going on with the Wilcoxson matched pairs test named object?

Al C
  • 5,175
  • 6
  • 44
  • 74
  • possible duplicate of https://stackoverflow.com/questions/51132277/weird-behaviour-when-iterating-through-list-and-deleting-elements-in-python – Dani Mesejo Sep 20 '18 at 20:27
  • 1
    As an aside, why are you defining a bunch of class-level variables (i.e. static variables) only to immediately shadow them with instance variables in your initializer? – juanpa.arrivillaga Sep 20 '18 at 20:31
  • But yeah, you are iterating a list while removing items from it, causing you to skip items. Generally, you simply create a new list to avoid the trickiness of iterating while mutating. It also happens to be significantly more efficient, because creating the new list will be O(N) whereas mutating the list this way will be O(N^2) – juanpa.arrivillaga Sep 20 '18 at 20:32
  • @juanpa.arrivillaga -- I'm defining and initializing the instance variables because I'm brand new to python, coming from an old statically typed language where it's common to do so :-) – Al C Sep 20 '18 at 20:36
  • The issue is the unecessary *class variables* not defining instance variables – juanpa.arrivillaga Sep 20 '18 at 20:45
  • Thank you all; I understand what's going on ... Out of curiosity, does python have a way to iterate downwards through a list, from the end backwards to index=0? – Al C Sep 20 '18 at 20:53

0 Answers0