[Tutor] how to remove the coming duplication
Asokan Pichai
pasokan at talentsprint.com
Thu Nov 10 08:23:43 CET 2011
On Thu, Nov 10, 2011 at 12:40 PM, lina <lina.lastname at gmail.com> wrote:
> Hi,
>
> How to remove the coming duplication,
>
> Here I wrote one (not working):
>
>
> >>> a=['2', '5', '7', '5', '5']
> >>> for i in range(len(a)-1):
> if a[i+1]==a[i]:
> a.remove(a[i+1])
> if i not in range(len(a)):
> break
>
>
> >>> a
> ['2', '7', '5', '5']
>
> I wish to get a is [2,5,7,5]
>
Did you mean [2, 5, 7]
just remove the coming duplication, not unique the list.
>
> Thanks for any advice,
>
I am assuming that you want to remove ALL duplicates.
Option 1:
Look at a different data structure -- one that
does not have duplicates by design: namely sets
Option 2:
Sort the list and then remove duplicates as you did.
look at sorted(a)
Option 3:
This is probably the least efficient but has
the merit of retaining the original order
Count the occurrence of each item and remove 1 less
look at a.count()
I have chosen to indicate the directions; hope it helps
Asokan Pichai
Enjoy programming in python
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111110/798a52c1/attachment.html>
More information about the Tutor
mailing list