473,218 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

program to eliminate any numbers which repeats themselves in a list

thatos
105 100+
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
Expand|Select|Wrap|Line Numbers
  1. >>> a = [1,1,1,5,10,10,16 ,16 , 20]
  2. >>> n = 0
  3. >>> b = len(a)
  4. >>> while n < b:
  5.     while a[n] == a[n+1]:
  6.         a.pop(n+1)
  7.     while a[n] != a[n+1]:
  8.         n += 1
  9.     b = len(a)
  10.  
  11.  
  12. 1
  13. 1
  14. 10
  15. 16
  16.  
  17. Traceback (most recent call last):
  18.   File "<pyshell#27>", line 4, in <module>
  19.     while a[n] != a[n+1]:
  20. IndexError: list index out of range
  21. >>> a
[1, 5, 10, 16, 20]
Oct 8 '07 #1
7 1595
elcron
43
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

Expand|Select|Wrap|Line Numbers
  1. >>> a = [1,1,1,5,10,10,16 ,16 , 20]
  2. >>> n = 0
  3. >>> b = len(a)
  4. >>> while n < b:
  5.     while a[n] == a[n+1]:
  6.         a.pop(n+1)
  7.     while a[n] != a[n+1]:
  8.         n += 1
  9.     b = len(a)
  10.  
  11.  
  12. 1
  13. 1
  14. 10
  15. 16
  16.  
  17. Traceback (most recent call last):
  18.   File "<pyshell#27>", line 4, in <module>
  19.     while a[n] != a[n+1]:
  20. IndexError: list index out of range
  21. >>> a
  22. [1, 5, 10, 16, 20]
  23.  
your code assumes that all duplicated numbers are next to each other. The error occurs on the last time through. This would fix it:
Expand|Select|Wrap|Line Numbers
  1. a = [1,1,1,5,10,10,16 ,16 , 20]
  2. index = 0
  3. length = len(a)
  4. while n < length:
  5.     while a[index] == a[index+1] and index+1 < length:
  6.         a.pop(n+1)
  7.     while a[index] != a[index+1]:
  8.         n += 1
  9.     length = len(a)
  10.  
Though if the order of the new list doesn't matter I would use:
Expand|Select|Wrap|Line Numbers
  1. oldList = [1,1,1,5,10,10,16 ,16 , 20]
  2. newList = list(set(oldList))
  3. #newList = [16, 1, 10, 20, 5]
  4.  
Or if order matters I would use the following function:
Expand|Select|Wrap|Line Numbers
  1. def removeDuplicates(oldList):
  2.     newList = []
  3.     for item in oldList:
  4.         if not item in newList:
  5.             newList.append(item)
  6.     return newList
  7.  
  8. oldList = [1,1,1,5,10,10,16 ,16 , 20]
  9. newList = removeDuplicates(oldList)
  10. #newList = [1, 5, 10, 16, 20]
  11.  
Oct 8 '07 #2
Smygis
126 100+
Expand|Select|Wrap|Line Numbers
  1. >>> lst = [random.randint(1, 100) for _ in xrange(1000)]
  2. >>> lst
  3. [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]
  4. >>> sorted(set(lst))
  5. [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]
  6.  
Oct 8 '07 #3
rhitam30111985
112 100+
This would do:
Expand|Select|Wrap|Line Numbers
  1.  a = [1,1,1,5,10,10,16 ,16 , 20]
  2.  for i in a:
  3.      if a.count(i)>=2:
  4.              a.remove(i)
  5.  
Oct 9 '07 #4
bvdet
2,851 Expert Mod 2GB
This is equivalent to elcron's removeDuplicates() function:
Expand|Select|Wrap|Line Numbers
  1. >>> a = [1,1,1,5,10,10,16,16,20]
  2. >>> [i for i in a if i not in locals()['_[1]']]
  3. [1, 5, 10, 16, 20]
  4. >>> 
Oct 9 '07 #5
thatos
105 100+
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:
Expand|Select|Wrap|Line Numbers
  1. a = [1,1,1,5,10,10,16 ,16 , 20]
  2. index = 0
  3. length = len(a)
  4. while n < length:
  5.     while a[index] == a[index+1] and index+1 < length:
  6.         a.pop(n+1)
  7.     while a[index] != a[index+1]:
  8.         n += 1
  9.     length = len(a)
  10.  
Though if the order of the new list doesn't matter I would use:
Expand|Select|Wrap|Line Numbers
  1. oldList = [1,1,1,5,10,10,16 ,16 , 20]
  2. newList = list(set(oldList))
  3. #newList = [16, 1, 10, 20, 5]
  4.  
Or if order matters I would use the following function:
Expand|Select|Wrap|Line Numbers
  1. def removeDuplicates(oldList):
  2.     newList = []
  3.     for item in oldList:
  4.         if not item in newList:
  5.             newList.append(item)
  6.     return newList
  7.  
  8. oldList = [1,1,1,5,10,10,16 ,16 , 20]
  9. newList = removeDuplicates(oldList)
  10. #newList = [1, 5, 10, 16, 20]
  11.  
Oct 9 '07 #6
bartonc
6,596 Expert 4TB
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.
Oct 9 '07 #7
bvdet
2,851 Expert Mod 2GB
This is similar to your approach:
Expand|Select|Wrap|Line Numbers
  1. def remove_dup(seq):
  2.     seq = seq[:]
  3.     idx = 0
  4.     n = len(seq)
  5.     while idx < n:
  6.         item = seq[idx]
  7.         if seq.count(item) > 1:
  8.             for i in range(seq.count(item)-1):
  9.                 # print 'Removing item at index %d' % seq.index(item, idx+1)
  10.                 seq.pop(seq.index(item, idx+1))
  11.                 n = len(seq)
  12.             idx += 1
  13.         else:
  14.             idx += 1
  15.     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.
Oct 10 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
8
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 `'}''...
4
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...
9
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...
27
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...
1
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...
0
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,...
9
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...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.