473,671 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

program to eliminate any numbers which repeats themselves in a list

thatos
105 New Member
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 1621
elcron
43 New Member
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 New Member
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 New Member
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 Recognized Expert Moderator Specialist
This is equivalent to elcron's removeDuplicate s() 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 New Member
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 Recognized Expert Expert
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 Recognized Expert Moderator Specialist
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,8 9,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
4957
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 integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help...
8
5389
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 `'}'' 'else if (choice == 27) exit(0) } }' Here is my program (Simple loop to display currency equivalencies based
4
2725
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 12345678,23456789,34567890... 2. check record-2 again record-1, check record-3 again record-2 & record-1 check record-4 again record-3 & record-2 & record-1 and so on... (if duplicated, drop it)
9
6886
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 Enter a number : 30 More numbers (y/n)? n
27
2169
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 clip of what I'm talking about. #include <stdio.h> #include <string.h>
1
3367
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 integers in the front of a double linked queue and the odd numbers in the back. Then prints out the deque. 2. Make a new deque list 2 that contain integers from 51 to 100. Print that list out as mine looks. Sort that list in decending order then print...
0
1142
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, students can’t be in a group with anyone they have been in a group with before or have a faculty member they have had before. Currently, there are three sets of groups that already exist. So, the program would need to be able to consult that data and...
9
5007
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 out BINGO numbers, but it can't make a repeat call out. Also, when something is called out, I have to somehow mark on the card that it has been called out. When the person gets 5 horizontally or vertically (not diagonally) then the programs says...
0
8473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8911
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.