9 Replies - 919 Views - Last Post: 05 April 2011 - 10:47 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

A problem with "set" and lists : removing items

Posted 05 April 2011 - 06:53 AM

Hi guys ,

I'm using the function "set" in order to remove multiple items from a list , and leave only one copy of each item . However ,the "set" function removes all copies except
for the left most copy , and I need to remove all copies except for the right most copy .

For instance if I have the following :
original => [1,2,3,4,1,5]
would become => [2,3,4,1,5]
and not =>[1,2,3,4,5] 


Any hints or ideas ? except of course for the long way of inner loops (already did that way , and it's a lot of code and I don't like it) Thanks in advance,Ron

This post has been edited by passer_by: 05 April 2011 - 06:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: A problem with "set" and lists : removing items

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 07:00 AM

How about reversing the list before you feed it into the set?
Was This Post Helpful? 0
  • +
  • -

#3 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 07:11 AM

View PostMotoma, on 05 April 2011 - 07:00 AM, said:

How about reversing the list before you feed it into the set?

It won't work.Set sorts them by their value ,i.e. from the lowest number (left) to the highest(right).
example :
A=[1,2,3,4,1,6100,44343,5]
A.reverse()    
B=set(A)    
print (B)/>              

>>> {1, 2, 3, 4, 5, 6100, 44343}


This post has been edited by passer_by: 05 April 2011 - 07:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 07:20 AM

If you're concerned about order, set probably isn't the best option.

It's a one liner, though...
>>> a =  [1,2,3,4,1,5]
>>> [ a[i] for i in range(len(a)) if a[i] not in a[:i] ]
[1, 2, 3, 4, 5]
>>> 


Was This Post Helpful? 1
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 07:20 AM

Oh, right.

I guess you'll have to remove them "the hard way": with a for loop:

newlist = []
for i in mylist[::-1]:
  if i not in newlist:
    newlist.insert(0, i)


Was This Post Helpful? 2
  • +
  • -

#6 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 07:28 AM

Okay ,I do not know about the use of
[-1::]  or [:i]
, but thank you guys , I'll check it out right now .

By the way,if you could explain the meaning of [-1::] or [:i] , I'll be grateful.

This post has been edited by passer_by: 05 April 2011 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 07:45 AM

What if you reversed the list, ran set, and then reversed again?

Old fashion way for the win.

This post has been edited by atraub: 05 April 2011 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 07:49 AM

mylist[element]
mylist[start:end]
mylist[start:end:step]

>>> mylist = [1,2,3,4,5,6]
>>> mylist[3]
4
>>> mylist[:3]
[1, 2, 3]
>>> mylist[3:]
[4, 5, 6]
>>> mylist[:]
[1, 2, 3, 4, 5, 6]
>>> mylist[::2]
[1, 3, 5]
>>> mylist[1::2]
[2, 4, 6]
>>> mylist[::-1]
[6, 5, 4, 3, 2, 1]
>>> 


Was This Post Helpful? 2
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 08:19 AM

http://docs.python.o...tion.html#lists
http://diveintopytho...ypes/lists.html
Was This Post Helpful? 0
  • +
  • -

#10 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: A problem with "set" and lists : removing items

Posted 05 April 2011 - 10:47 AM

View PostMotoma, on 05 April 2011 - 07:49 AM, said:

mylist[element]
mylist[start:end]
mylist[start:end:step]

>>> mylist = [1,2,3,4,5,6]
>>> mylist[3]
4
>>> mylist[:3]
[1, 2, 3]
>>> mylist[3:]
[4, 5, 6]
>>> mylist[:]
[1, 2, 3, 4, 5, 6]
>>> mylist[::2]
[1, 3, 5]
>>> mylist[1::2]
[2, 4, 6]
>>> mylist[::-1]
[6, 5, 4, 3, 2, 1]
>>> 


Well,this is very impressive.I really did not know that,reminds me a little about the syntax of Matlab.

Great post,10x a lot guys (:
Ron

This post has been edited by passer_by: 05 April 2011 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1