need help I was trying to do a program which could eliminate numbers which repeats themselves in a list the program below works but it gives me an error can you please try to find the problem as have I tried to check where the problem is but i couldn't find the solution -
>>> a = [1,1,1,5,10,10,16 ,16 , 20]
-
>>> n = 0
-
>>> b = len(a)
-
>>> while n < b:
-
while a[n] == a[n+1]:
-
a.pop(n+1)
-
while a[n] != a[n+1]:
-
n += 1
-
b = len(a)
-
-
-
1
-
1
-
10
-
16
-
-
Traceback (most recent call last):
-
File "<pyshell#27>", line 4, in <module>
-
while a[n] != a[n+1]:
-
IndexError: list index out of range
-
>>> a
[1, 5, 10, 16, 20]
7 1600
need help I was trying to do a program which could eliminate numbers which repeats themselves in a list the program below works but it gives me an error can you please try to find the problem as have I tried to check where the problem is but i couldn't find the solution -
>>> a = [1,1,1,5,10,10,16 ,16 , 20]
-
>>> n = 0
-
>>> b = len(a)
-
>>> while n < b:
-
while a[n] == a[n+1]:
-
a.pop(n+1)
-
while a[n] != a[n+1]:
-
n += 1
-
b = len(a)
-
-
-
1
-
1
-
10
-
16
-
-
Traceback (most recent call last):
-
File "<pyshell#27>", line 4, in <module>
-
while a[n] != a[n+1]:
-
IndexError: list index out of range
-
>>> a
-
[1, 5, 10, 16, 20]
-
your code assumes that all duplicated numbers are next to each other. The error occurs on the last time through. This would fix it: -
a = [1,1,1,5,10,10,16 ,16 , 20]
-
index = 0
-
length = len(a)
-
while n < length:
-
while a[index] == a[index+1] and index+1 < length:
-
a.pop(n+1)
-
while a[index] != a[index+1]:
-
n += 1
-
length = len(a)
-
Though if the order of the new list doesn't matter I would use: -
oldList = [1,1,1,5,10,10,16 ,16 , 20]
-
newList = list(set(oldList))
-
#newList = [16, 1, 10, 20, 5]
-
Or if order matters I would use the following function: -
def removeDuplicates(oldList):
-
newList = []
-
for item in oldList:
-
if not item in newList:
-
newList.append(item)
-
return newList
-
-
oldList = [1,1,1,5,10,10,16 ,16 , 20]
-
newList = removeDuplicates(oldList)
-
#newList = [1, 5, 10, 16, 20]
-
-
>>> lst = [random.randint(1, 100) for _ in xrange(1000)]
-
>>> lst
-
[70, 91, 80, 67, 77, 25, 20, 59, 52, 15, 88, 42, 26, 32, 22, 73, 79, 61, 56, 71, 30, 35, 41, 34, 33, 36, 2, 42, 56, 74, 31, 84, 73, 88, 81, 76, 29, 43, 93, 77, 82, 59, 63, 8, 88, 75, 34, 4, 51, 90, 99, 6, 9, 88, 55, 70, 46, 29, 91, 36, 12, 52, 30, 90, 88, 6, 42, 19, 14, 87, 5, 41, 66, 50, 76, 63, 11, 15, 9, 71, 68, 36, 100, 31, 91, 54, 29, 97, 13, 83, 20, 22, 37, 64, 49, 56, 42, 38, 28, 10, 94, 18, 81, 79, 27, 91, 58, 12, 18, 69, 79, 98, 11, 75, 6, 94, 88, 75, 18, 53, 26, 33, 29, 22, 57, 37, 66, 5, 81, 99, 71, 32, 99, 39, 1, 94, 5, 70, 16, 100, 91, 59, 71, 36, 59, 64, 22, 50, 53, 84, 73, 3, 67, 61, 81, 86, 59, 38, 1, 70, 48, 38, 74, 5, 32, 80, 21, 73, 82, 82, 29, 6, 98, 76, 34, 13, 68, 51, 28, 53, 92, 93, 68, 26, 6, 54, 48, 95, 21, 97, 22, 42, 96, 50, 32, 89, 72, 15, 21, 5, 98, 52, 32, 43, 22, 73, 88, 3, 63, 71, 79, 43, 31, 67, 68, 83, 79, 97, 71, 52, 51, 58, 46, 71, 77, 40, 58, 41, 63, 34, 58, 93, 37, 76, 8, 70, 66, 25, 57, 60, 2, 21, 38, 62, 30, 13, 24, 99, 6, 3, 17, 25, 51, 13, 61, 27, 49, 97, 7, 46, 16, 72, 97, 27, 14, 27, 47, 30, 50, 63, 31, 66, 34, 82, 94, 28, 23, 58, 76, 21, 61, 51, 61, 33, 49, 80, 53, 30, 57, 68, 10, 93, 22, 6, 78, 53, 43, 81, 63, 15, 1, 50, 92, 28, 45, 59, 27, 39, 78, 71, 32, 51, 63, 85, 2, 15, 17, 95, 29, 25, 99, 45, 29, 72, 67, 73, 12, 51, 20, 78, 35, 44, 48, 31, 23, 19, 33, 66, 14, 11, 41, 88, 88, 29, 75, 22, 3, 53, 76, 7, 2, 46, 63, 97, 21, 38, 4, 29, 59, 27, 47, 38, 58, 45, 83, 45, 27, 25, 26, 66, 49, 2, 10, 6, 25, 76, 59, 83, 34, 53, 13, 24, 30, 49, 17, 100, 82, 23, 96, 18, 14, 64, 82, 39, 80, 45, 79, 50, 13, 31, 31, 27, 60, 84, 95, 81, 28, 19, 33, 62, 62, 42, 5, 18, 69, 40, 66, 57, 53, 99, 3, 75, 54, 99, 76, 65, 63, 12, 81, 54, 71, 79, 68, 29, 44, 21, 43, 92, 98, 34, 34, 100, 62, 38, 65, 76, 84, 80, 68, 70, 92, 68, 40, 100, 89, 67, 83, 26, 76, 13, 39, 57, 80, 54, 54, 92, 81, 79, 7, 93, 18, 32, 80, 11, 67, 4, 73, 6, 66, 31, 89, 50, 1, 73, 6, 10, 64, 82, 38, 92, 40, 63, 46, 20, 74, 82, 29, 74, 7, 63, 15, 59, 87, 6, 82, 29, 60, 24, 58, 6, 80, 34, 48, 88, 16, 49, 74, 54, 75, 36, 26, 19, 70, 87, 94, 91, 27, 59, 93, 9, 70, 71, 47, 44, 91, 83, 77, 49, 32, 22, 75, 93, 43, 68, 82, 33, 35, 55, 14, 10, 6, 88, 54, 1, 22, 12, 45, 65, 68, 32, 22, 6, 84, 85, 12, 51, 44, 85, 76, 57, 82, 31, 46, 39, 38, 33, 24, 22, 34, 55, 32, 76, 28, 70, 49, 66, 94, 75, 22, 37, 87, 52, 62, 44, 43, 49, 76, 37, 92, 22, 75, 100, 63, 89, 74, 48, 25, 35, 57, 71, 33, 6, 60, 92, 89, 25, 42, 10, 79, 60, 5, 19, 26, 84, 56, 60, 30, 88, 31, 11, 57, 59, 37, 72, 48, 69, 84, 92, 30, 92, 100, 33, 11, 83, 50, 58, 59, 22, 8, 87, 97, 60, 40, 74, 35, 45, 21, 12, 50, 4, 56, 93, 16, 79, 9, 18, 22, 48, 100, 89, 38, 37, 95, 75, 71, 44, 69, 68, 99, 78, 63, 99, 58, 6, 1, 28, 6, 65, 64, 19, 48, 59, 16, 61, 74, 33, 95, 56, 21, 76, 45, 13, 32, 38, 2, 39, 61, 87, 12, 80, 2, 68, 6, 10, 73, 68, 85, 53, 44, 66, 12, 17, 95, 97, 97, 18, 52, 51, 58, 97, 70, 72, 68, 62, 40, 82, 35, 4, 3, 17, 90, 2, 88, 28, 5, 42, 30, 58, 82, 73, 73, 53, 49, 16, 95, 52, 21, 44, 39, 32, 33, 31, 81, 46, 71, 45, 81, 9, 76, 21, 55, 59, 65, 53, 45, 74, 56, 34, 98, 68, 64, 8, 14, 43, 63, 38, 31, 25, 54, 45, 69, 77, 16, 8, 63, 4, 99, 22, 73, 63, 26, 65, 20, 41, 69, 33, 2, 10, 3, 92, 65, 23, 95, 38, 71, 20, 24, 5, 85, 23, 83, 60, 32, 21, 55, 40, 30, 6, 45, 13, 46, 21, 31, 97, 87, 40, 72, 84, 61, 61, 96, 75, 7, 40, 28, 66, 39, 68, 96, 44, 97, 20, 51, 39, 46, 67, 65, 4, 48, 89, 7, 22, 35, 4, 55, 43, 84, 81, 49, 98, 78, 28, 66, 41, 74, 10, 29, 33, 18, 72, 16, 9, 45, 73, 21, 18, 89, 20, 50, 40, 94, 93, 96, 98, 62, 20, 69, 14, 40, 45, 29, 47, 65, 68, 18, 75, 81, 66, 26, 12, 4, 21, 53, 14, 12, 18, 79, 50, 84, 4, 99, 26, 19, 72, 72, 41, 17, 24, 79, 90, 36, 56, 55, 3, 29, 88, 36, 26, 33, 25, 63, 11, 75, 15, 22, 40, 31, 27, 18, 4, 37, 49, 14, 85, 43, 86, 42, 18, 51, 73, 49, 100, 33, 81, 1, 45, 31, 92, 74, 38, 44, 89, 24, 23, 15, 33, 11, 66, 76, 21, 18, 19, 73, 20, 43, 37, 22, 4, 30, 80, 93, 100, 73, 42, 10, 87, 26, 30, 73, 26]
-
>>> sorted(set(lst))
-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
-
This would do: -
a = [1,1,1,5,10,10,16 ,16 , 20]
-
for i in a:
-
if a.count(i)>=2:
-
a.remove(i)
-
bvdet 2,851
Expert Mod 2GB
This is equivalent to elcron's removeDuplicates() function: - >>> a = [1,1,1,5,10,10,16,16,20]
-
>>> [i for i in a if i not in locals()['_[1]']]
-
[1, 5, 10, 16, 20]
-
>>>
They code which you posted did'n work it still gives me an error of list index out of range
your code assumes that all duplicated numbers are next to each other. The error occurs on the last time through. This would fix it: -
a = [1,1,1,5,10,10,16 ,16 , 20]
-
index = 0
-
length = len(a)
-
while n < length:
-
while a[index] == a[index+1] and index+1 < length:
-
a.pop(n+1)
-
while a[index] != a[index+1]:
-
n += 1
-
length = len(a)
-
Though if the order of the new list doesn't matter I would use: -
oldList = [1,1,1,5,10,10,16 ,16 , 20]
-
newList = list(set(oldList))
-
#newList = [16, 1, 10, 20, 5]
-
Or if order matters I would use the following function: -
def removeDuplicates(oldList):
-
newList = []
-
for item in oldList:
-
if not item in newList:
-
newList.append(item)
-
return newList
-
-
oldList = [1,1,1,5,10,10,16 ,16 , 20]
-
newList = removeDuplicates(oldList)
-
#newList = [1, 5, 10, 16, 20]
-
They code which you posted did'n work it still gives me an error of list index out of range
elcron has provided two solutions which do work and are much more elegant.
The first "solution" isn't really worth the effort to fix it, though.
bvdet 2,851
Expert Mod 2GB
This is similar to your approach: - def remove_dup(seq):
-
seq = seq[:]
-
idx = 0
-
n = len(seq)
-
while idx < n:
-
item = seq[idx]
-
if seq.count(item) > 1:
-
for i in range(seq.count(item)-1):
-
# print 'Removing item at index %d' % seq.index(item, idx+1)
-
seq.pop(seq.index(item, idx+1))
-
n = len(seq)
-
idx += 1
-
else:
-
idx += 1
-
return seq
>>> a = [1,3,6,4,3,2,7,89,6,5,4,7,6,2,7,6,8,9,0,6,5,4,4,3,2 ,1,2,3,1,2,3,6,7,8,5,4,3,2,1,5,4,8]
>>> remove_dup(a)
[1, 3, 6, 4, 2, 7, 89, 5, 8, 9, 0]
>>>
This is not the best solution though.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: hoover_richard |
last post by:
I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd...
|
by: drose0927 |
last post by:
Please help! I can't get my program to exit if the user hits the
Escape button:
When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this
error:
Parse Error, expecting `'}''...
|
by: Tony WONG |
last post by:
i have a number of forms with fax numbers to come up into arrays and then
combine to string.
after that i design the flow
1. break the string to array
now the string looks like this...
|
by: ehabaziz2001 |
last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal
program termination"
E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01
Enter a number : 25
More numbers (y/n)? y...
|
by: Neil |
last post by:
Hello all!
I wrote program with a array of pointers, and I suspect they are
pointing at each other in the Do ...While loop.
Something is messed up with the increment variable word. A program...
|
by: Homeworkboy |
last post by:
Can anyone help me with this program?
Look at the bottom of this program for help methods:
/*1. Make a program that uses numbers from 1 to 100 including each ends which puts the even...
|
by: DesperateStdnt |
last post by:
Basically, the program needs to take a data set of X (i.e. 80) students and assign them them into Y (i.e. 10) groups. Then assign Y (i.e. 10) number of faculty members, one to each group. But,...
|
by: Fsunka |
last post by:
Hey, I have a project due around February 28. I have to create a program that asks the user for the number of players that want to play bingo. Then it creates that many bingo cards. Then it calls...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |