Reputation: 893
I am trying to divide a number in a list by some of the numbers in the same list.
This is my code:
for i in numbers:
list_position = numbers.index(i)
half_list_position = int(list_position//2 + 1)
if i % numbers[0:half_list_position] in numbers == 0:
prime_list.remove(i)
As you can see I want to divide 'i' by half of the numbers that have come before it for remainder zero and am trying to do this by using list indexes. If at any point 'i' divided by one of the numbers in the list returns remainder zero I want to remove the same number 'i' from another list (prime_list) then move onto the next number in the list 'numbers'.
This returns a type error: unsupported operand type.
How can I get around this?
Thanks,
Ben
Upvotes: 0
Views: 90
Reputation: 97918
A list slice is a list and modulus is not supported between numbers and lists ([0 : half_list_position]
is not even a slice):
i % [0:half_list_position]
You can do something like this:
if sum(1 for n in numbers[:half_list_position] if i % n == 0) > 0:
prime_list.remove(i)
Upvotes: 2