In the list you see as an output, notice after 7, every number is repeated. From
ID: 3719450 • Letter: I
Question
In the list you see as an output, notice after 7, every number is repeated. From line 31 I am trying to create code that says if the current number == next number, remove the current number. For some reason python is telling me that my index for my list is out of range. Can someone please help me with this trivial code?
/problem3.ipynb problem3 (unsaved changes) View Insert Cell Kernel Widgets Help Trusted rotate get-rotations (p) pos np. arange (0, 1en (rotate)-2, 1) for b in np. arange (0, len (rotate)-1,1): 24 25 26 27 28 29 30 31 if findprimes (rotate [b]) and findprimes (rotate[b+1]): for k in rotate: rotat. append (rotate [b]) print (rotat) for 11 in np. arange (0, len (rotat)-1, 1): 32 if rotat[11]=-rotat [11+1]: rotat. remove (rotat[il1]) 34 36 37 38 print (circular_primes (100)) [2. 3, 5, 7, 11, 11, 13, 13, 17, 17. 31, 31, 37, 37, 71 71, 73, 73, 79, 79, 97, 97] IndexError Kipython-input-17-36f3c6957edb> in module O Traceback (most recent call last) 36Explanation / Answer
By seeing the python i absorbed that your try to remove the duplicates in a list.....your logic is fine....but it is not a correct approach....for each iteration your checking & removing the duplicates...after removing one duplicate index there is a difference in the range of the loop so that why it is showing out of range..........I modified your approach....my approach i am taking another list...try to append the numbers without duplicates.....the code snippet is below...please find that...
///////////////////Snippet/////////////////
new_list = []
for ll in np.arrange(0,length(rotat)-1,1):
if rotat[ll] not in new_list:
new_list.append(num)
use this non dupliacte list to further operations.....Happy coding
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.