Python modifying wrong list? -


i'm trying generate list of primes using this method. need loop through every number 2...n , check multiples of 2...n. reason, wrong list seems getting modified.

import sys import argparse import math  parser = argparse.argumentparser(description='find largest prime factor of number') parser.add_argument('n', type=int, help='number') args = parser.parse_args()  sieve = [] in range(2,args.n+1): sieve.append(i) # tried int(i)   copy1 = sieve # tried making copies. . . copy2 = sieve copy3 = sieve #print int(math.sqrt(args.n))  index, in enumerate(copy1):     #print index,     ii in copy2:         #print ii         if % ii == 0:             sieve[index]= none   print sieve 

i following error:

traceback (most recent call last):     file "3.py", line 22, in <module>     if % ii == 0: typeerror: unsupported operand type(s) %: 'int' , 'str' 

you're not making copies. you're using references, copy1, copy2, , copy3 refer same list -- sieve. if want copy, use:

copy1 = sieve[:] 

which create copy of sieve , assign copy1.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -