lets say i have a list of random numbers
randomlist = [9, 8, 18, 1, 17, 3, 13, 4, 13, 1, 9, 10, 7, 13, 3, 9, 13, 10, 18, 10, 19, 3, 14, 14, 19, 4, 20, 17, 8, 17, 3, 12, 8, 12, 1, 2, 15, 13, 2, 8, 18, 10, 14, 11, 17, 11, 2, 7, 4, 7, 5, 5, 18, 7, 11, 13, 20, 9, 2, 14, 19, 1, 16, 10, 16, 19, 13, 19, 11, 17, 8, 2, 10, 16, 5, 14, 7, 11, 17, 9, 9, 6, 12, 6, 12, 4, 14, 10, 2, 6, 9, 1, 14, 4, 14, 13, 18, 13, 6, 8]
There are three categorys of numbers: numbers which are divisible by two (2,4,6,8,10,12,14,16,18,20) prime numbers except two (3,7,11,13,17,19) rest (1,5,9,15)
Now what i want to do is the following: I will go through the list and whenever a number of the rest category occurs i want to create a list like this [ %2 , prime] whereas the elements are the ones which occured most recently in the list. The goal is to have a lists of lists with two elements. I hope its clear what im trying to do. Here is my code:
randomlist = [9, 8, 18, 1, 17, 3, 13, 4, 13, 1, 9, 10, 7, 13, 3, 9, 13, 10, 18, 10, 19, 3, 14, 14, 19, 4, 20, 17, 8, 17, 3, 12, 8, 12, 1, 2, 15, 13, 2, 8, 18, 10, 14, 11, 17, 11, 2, 7, 4, 7, 5, 5, 18, 7, 11, 13, 20, 9, 2, 14, 19, 1, 16, 10, 16, 19, 13, 19, 11, 17, 8, 2, 10, 16, 5, 14, 7, 11, 17, 9, 9, 6, 12, 6, 12, 4, 14, 10, 2, 6, 9, 1, 14, 4, 14, 13, 18, 13, 6, 8]
def check_prime(x):
for i in range(2, x):
if (x % i) == 0:
return False
else:
return True
def check_number(x):
if x%2 == 0:
return "zweier"
elif check_prime(x) == True:
return "prim"
else:
return "rest"
final_list = []
partial_list = [0,0]
for x in randomlist:
if check_number(x) =="zweier":
partial_list[0] = x
elif check_number(x) == "rest":
partial_list[1] = x
else:
final_list.append(partial_list)
for x in final_list:
print(x)
Ouput:
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
What confuses me very much is that if i print the final_list members right after creating them it shows the right solution:
randomlist = [9, 8, 18, 1, 17, 3, 13, 4, 13, 1, 9, 10, 7, 13, 3, 9, 13, 10, 18, 10, 19, 3, 14, 14, 19, 4, 20, 17, 8, 17, 3, 12, 8, 12, 1, 2, 15, 13, 2, 8, 18, 10, 14, 11, 17, 11, 2, 7, 4, 7, 5, 5, 18, 7, 11, 13, 20, 9, 2, 14, 19, 1, 16, 10, 16, 19, 13, 19, 11, 17, 8, 2, 10, 16, 5, 14, 7, 11, 17, 9, 9, 6, 12, 6, 12, 4, 14, 10, 2, 6, 9, 1, 14, 4, 14, 13, 18, 13, 6, 8]
def check_prime(x):
for i in range(2, x):
if (x % i) == 0:
return False
else:
return True
def check_number(x):
if x%2 == 0:
return "zweier"
elif check_prime(x) == True:
return "prim"
else:
return "rest"
final_list = []
partial_list = [0,0]
for x in randomlist:
if check_number(x) =="zweier":
partial_list[0] = x
elif check_number(x) == "rest":
partial_list[1] = x
else:
final_list.append(partial_list)
print(final_list[-1])
Output:
[18, 9]
[18, 9]
[18, 9]
[18, 9]
[4, 9]
[4, 9]
[10, 9]
[10, 9]
[10, 9]
[10, 9]
[10, 9]
[10, 9]
[14, 9]
[20, 9]
[8, 9]
[8, 9]
[12, 9]
[2, 15]
[14, 15]
[14, 15]
[14, 15]
[2, 15]
[4, 15]
[4, 15]
[4, 15]
[18, 15]
[18, 15]
[18, 15]
[14, 9]
[14, 9]
[16, 9]
[16, 9]
[16, 9]
[16, 9]
[16, 9]
[16, 9]
[14, 9]
[14, 9]
[14, 9]
[6, 9]
[14, 9]
[18, 9]
So it seems like the basis idea is okay. I tried very much but i just cant find out what i am doing wrong. Thanks very much for help !