abuteau
abuteau

Reputation: 7441

Removing last instance of duplicates

Removing duplicates from a list in Python is easy (order preserving):

def removeDuplicates(sequence):    
    checked = []
    for element in sequence:
        if element not in checked:
            checked.append(element)
    return checked

But if I want to remove remove the last instance of the duplicates (ie: [1,1,1,2,2,2] -> [1,1,2,2]), how can I do it?

Upvotes: 1

Views: 1340

Answers (5)

Mingyu
Mingyu

Reputation: 33349

1 - Traverse the list, and add each element into a dictionary, let's call it duplicateMap.

Key: element in the list

Value: count of element

2 - Traverse the list again from the back.

For each element, check

1) if duplicateMap contains the element;
2) if the count is greater than 1.

If yes,

1) remove the element from the list;
2) remove the element from duplicateMap.

Upvotes: 2

Thothadri Rajesh
Thothadri Rajesh

Reputation: 522

How About like this

def removelastduplicate(s):
  len_s=len(s)
  checked=[]
  for i in range(0,len_s):
    number=s.pop(0)
    if number in s: # the last occurance wont be present in the list, so not added
      checked.append(number)
  return checked

s=[1,1,1,2,2,2]
print removelastduplicate(s)

Upvotes: 1

Harpal
Harpal

Reputation: 12577

My python isn't too great but how about this:

>>> l = [1,1,1,2,2,2]
>>> last_occ=[len(l) - 1 - l[::-1].index(i) for i in set(l)] # Find position of each last occurence
>>> for pos in last_occ[::-1]: # Reverse the occurrence list otherwise you may get an IndexError 
    l.pop(pos)
>>> l
[1, 1, 2, 2]

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142126

How about:

from collections import OrderedDict
from itertools import chain

data = [
    ['Jim', 18],
    ['James', 19],
    ['Bob', 20],
    ['Jim', 15],
    ['Bob', 55],
    ['Jim', 99],
    ['Single', 123]
]

od = OrderedDict()
for el in data:
    od.setdefault(el[0], []).append(el)

deduped = list(chain.from_iterable(item[:-1] if len(item) > 1 else item for item in od.itervalues()))
# [['Jim', 18], ['Jim', 15], ['James', 19], ['Bob', 20], ['Single', 123]]

This uses names and ages as example data and dedupes based on the name - which is a bit more interesting than just numbers... We append them to a list each and at the end take all the elements and put them back into order of presented keys grouped together.

Upvotes: 1

John
John

Reputation: 490

Ok, my mind is in Javascript mode right now, so the code isn't coming to me off the top of my head, but conceptually, the idea that first comes to my mind is:

for x in originalList  of [A,A,B,B,A,B,C,C]
store x as entry in dictionary {A:[0,1,4];B:[2,3,5];C:[6,7]}
Then loop through all of the lists in the dictionary and pull the max value
    from each and push it to a new list that you then reverse sort, 
    ex output [7,5,4]
Then for each value in the resulting list, remove the value at that place in the original list
    (Do it in the largest to least order though, that way as you remove values, its not changing the value of the other entries that need to be removed)

There's probably a better way to do this, and I'm sorry I don't have the code for you off hand for that idea, but I hope the concept helps, if you need me to further explain what I mean, just let me know.

Upvotes: 0

Related Questions