program to eliminate any numbers which repeats themselves in a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    program to eliminate any numbers which repeats themselves in a list

    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
    Code:
    >>> 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]
    Last edited by bartonc; Oct 8 '07, 06:04 PM. Reason: Added [CODE][/CODE] tags.
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    Originally posted by thatos
    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

    [code=python]
    >>> a = [1,1,1,5,10,10,1 6 ,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#2 7>", line 4, in <module>
    while a[n] != a[n+1]:
    IndexError: list index out of range
    >>> a
    [1, 5, 10, 16, 20]
    [/code]
    your code assumes that all duplicated numbers are next to each other. The error occurs on the last time through. This would fix it:
    [code=python]
    a = [1,1,1,5,10,10,1 6 ,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)
    [/code]
    Though if the order of the new list doesn't matter I would use:
    [code=python]
    oldList = [1,1,1,5,10,10,1 6 ,16 , 20]
    newList = list(set(oldLis t))
    #newList = [16, 1, 10, 20, 5]
    [/code]
    Or if order matters I would use the following function:
    [code=python]
    def removeDuplicate s(oldList):
    newList = []
    for item in oldList:
    if not item in newList:
    newList.append( item)
    return newList

    oldList = [1,1,1,5,10,10,1 6 ,16 , 20]
    newList = removeDuplicate s(oldList)
    #newList = [1, 5, 10, 16, 20]
    [/code]

    Comment

    • Smygis
      New Member
      • Jun 2007
      • 126

      #3
      [code=python]
      >>> 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]
      [/code]

      Comment

      • rhitam30111985
        New Member
        • Aug 2007
        • 112

        #4
        This would do:
        [CODE=python]
        a = [1,1,1,5,10,10,1 6 ,16 , 20]
        for i in a:
        if a.count(i)>=2:
        a.remove(i)
        [/CODE]

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          This is equivalent to elcron's removeDuplicate s() function:[code=Python]>>> a = [1,1,1,5,10,10,1 6,16,20]
          >>> [i for i in a if i not in locals()['_[1]']]
          [1, 5, 10, 16, 20]
          >>> [/code]

          Comment

          • thatos
            New Member
            • Aug 2007
            • 105

            #6
            They code which you posted did'n work it still gives me an error of list index out of range

            Originally posted by elcron
            your code assumes that all duplicated numbers are next to each other. The error occurs on the last time through. This would fix it:
            [code=python]
            a = [1,1,1,5,10,10,1 6 ,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)
            [/code]
            Though if the order of the new list doesn't matter I would use:
            [code=python]
            oldList = [1,1,1,5,10,10,1 6 ,16 , 20]
            newList = list(set(oldLis t))
            #newList = [16, 1, 10, 20, 5]
            [/code]
            Or if order matters I would use the following function:
            [code=python]
            def removeDuplicate s(oldList):
            newList = []
            for item in oldList:
            if not item in newList:
            newList.append( item)
            return newList

            oldList = [1,1,1,5,10,10,1 6 ,16 , 20]
            newList = removeDuplicate s(oldList)
            #newList = [1, 5, 10, 16, 20]
            [/code]

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by thatos
              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.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                This is similar to your approach:[code=Python]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.ind ex(item, idx+1))
                n = len(seq)
                idx += 1
                else:
                idx += 1
                return seq[/code]

                >>> 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.

                Comment

                Working...