Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem selecting an item from the list in the loop

def counter(x) :
    counter_list = []
    for i in range(2 , int(x**0.5) + 1) :
        if x % i == 0 :
            counter_list.append(i)
    return counter_list

counter() check the counter of any number and return the counters as list

counter_list = counter(600851475143)
print(counter_list)

i get the counters of 600851475143 and I want to separate the prime numbers

for x in counter_list :
    #-----104441 is counter of 600851475143 but when But it is not placed in loop equal to the variable x, in other words, it is not read at all
    print("..////////////////////////////////////////////" , x)
    print(104441 in counter_list)
    for i in range(2 , int(x**0.5) + 1) :

        if x % i == 0 :
            counter_list.remove(x)

My problem is that the number 104441 is in the list, but it is not read when it is used in a loop

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try to create a copy of your list. If you don’t do this, you are changing the list you are iterating over. This will cause the loop to behave in a wrong way.

# create a copy of your list
copy_counter_list = counter_list.copy()

for x in counter_list :
    #-----104441 is counter of 600851475143 but when But it is not placed in loop equal to the variable x, in other words, it is not read at all
    print("..////////////////////////////////////////////" , x)
    print(104441 in counter_list)
    for i in range(2 , int(x**0.5) + 1) :

        if x % i == 0 :
            copy_counter_list.remove(x)

Your result will then be in copy_counter_list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading