Andra
Andra

Reputation: 47

Sets looping and operating in Python

How do I loop through two sets and make operations with each of the sets items. Or I can make them lists or tuple if necessary, I thought about sets since they don't have duplicates However, I believe that is impossible...and it should be other way. Here's my code, is fairly explanatory, and it works until it reaches the sets, obviously...:

def p2():
    myPrimes = set()
    myPossiblePrimes= set()
    myDividersList= set()


    for x in range(2,11):
        for y in range(1,5):
            if x%y != 0:
                print (x,'does not equally divide with',y,'and I added ',x,'into the possible set')
                myPossiblePrimes.add(x)
                myDividersList.add(y)
                def f():
                    for a in range (myPossiblePrimes):
                        for b in range (myPossibleDividers):
                            if a%b == 0:
                                myPossiblePrimes.remove(x)
                                return[myPossiblePrimes]

                filter(f(), myPossiblePrimes)
                print ('checked')
    return[list(enumerate(myPossiblePrimes))]

Upvotes: 0

Views: 72

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122032

for a in range (myPossiblePrimes):

Mixes up two kinds of for loop:

for item in iterable:

for index in range(integer): # or range(len(iterable))

You just need:

for a in myPossiblePrimes:

A few other issues:

  1. A function for filter should take one argument (each item in the iterable being filtered) and return either True (keep item) or False (remove item).

  2. "myPossibleDividers" != "myDividersList"

  3. filter(f(), myPossiblePrimes) should be filter(f, myPossiblePrimes)

Upvotes: 1

Related Questions