简体   繁体   中英

How to iterate through Tuple<List<String>> in Python

class App:
    def filter(*input):
        result = []
        print(type(input))
        for arrayOfColors in input:
            print(type(arrayOfColors))
            goodColors = getGoodColors(arrayOfColors)
            result.add(goodColors)
        return result

def getGoodColors(arrayOfColors):
    setOfNotGoodColors = ["gray", "yellow", "purple"]
    goodColors = []
    for color in arrayOfColors:
        if color not in setOfNotGoodColors:
            goodColors.add(color)
    return goodColors
        
app = App()
app.filter(["blue", "red"], ["gray","blue"])

The result of compilation is:

<class '__main__.App'>
Traceback (most recent call last):
  File "<string>", line 20, in <module>
File "<string>", line 7, in filter
  File "<string>", line 14, in getGoodColors
TypeError: 'App' object is not iterable

How to get rid of this error? I have to write more not code characters, please help.

You can see the problem if you add print(input) right under def filter(*input): ; you'll see

(<__main__.App object at 0x00000211E39C0A48>, ['blue', 'red'], ['gray', 'blue'])

That is the result of self in every class. You can avoid it in your for loop with a slice of [1:] . Also, python list s have no attribute called add ; they are called append :

class App:
    def filter(*input):
        result = []
        for arrayOfColors in input[1:]:
            goodColors = getGoodColors(arrayOfColors)
            result.append(goodColors)
        return result

def getGoodColors(arrayOfColors):
    setOfNotGoodColors = ["gray", "yellow", "purple"]
    goodColors = []
    for color in arrayOfColors:
        if color not in setOfNotGoodColors:
            goodColors.append(color)
    return goodColors
        
app = App()
app.filter(["blue", "red"], ["gray","blue"])

Note that it is a bad practice to name your variables names that are taken by built-ins, so that input variable would be better off named as my_input .

Inside a python class every method should have a self parameter, for example:

class App:
    def filter(self, *input):

with the self parameter you can reach the properties of that instance.

another problem is: in order to add a value to a python list you need to use the function append , for example:

goodColors.append(color)

Your final code should look something like this:

class App:
    def filter(self, *input):
        result = []
        print(type(input))
        for arrayOfColors in input:
            print(type(arrayOfColors))
            goodColors = getGoodColors(arrayOfColors)
            result.append(goodColors)
        return result

def getGoodColors(arrayOfColors):
    setOfNotGoodColors = ["gray", "yellow", "purple"]
    goodColors = []
    for color in arrayOfColors:
        if color not in setOfNotGoodColors:
            goodColors.append(color)
    return goodColors
        
app = App()
app.filter(["blue", "red"], ["gray","blue"])

Note: You shouldn't use the name input as a paramter because it is the name of a built-in function, but the code runs even if you use it so I didn't fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM