Edit: Resolved, I just was thinking wrong and the function never reached the second return for numbers bigger than n, so no need for anyone here to read it.
I know this is a "duplicate" - but not entirely. Currently I am using Python 3.6.2
I generate a list of primes and then want to copy it, so I can modify it to find a specific prime and also return the list of primes.
I tried:
new_list = old_list[:]
new_list = list(old_list)
new_list = old_list * 1
new_list = old_list.copy()
new_list = [char for char in old_list]
But NONE of those work. In my code I reduce the list of primes to just an integer, and then return the number and the list. But all modifications done to the old_list are done to the new_list, regardless of the method used.
Code in question:
def f(n):
#generate primelist
primes = []
numbers = set(range(n,1,-1))
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2,n+1,p)))
#any of these, none work
primelist = primes[:]
primelist = primes * 1
primelist = primes.copy()
primelist = list(primes)
primelist = [char for char in primes]
#find even digits
import re
evendigs = re.compile(r'[24680]')
longest = []
prime = 0
#remove n from primes if n is prime
if n in primes:
primes.remove(n)
#find largest prime with maximum number of even digits that is smaller than n
for i in list(reversed(primes)):
if len(str(prime))> len(str(i)):
return prime
evenprime = re.findall(evendigs,str(i))
if evenprime:
if len(evenprime)>len(longest):
longest = evenprime
prime = i
elif len(evenprime)==len(longest):
longest = evenprime
if i > prime:
prime = i
else: continue
return (primelist, primes)
Also why does:
a,b = f(9451) raise the error: int object is not iterable?
assuming i only return e.g. primelist (which should be a list of primes up to n):
number = f(9451) but a = 8243 and a is not a list
returning primes (which should be a number, even though the number is wrong), gives the same value.