How to remove lists that are the same after reversed?

This is the place for queries that don't fit in any of the other categories.

How to remove lists that are the same after reversed?

Postby birda » Thu Mar 14, 2013 4:41 pm

Hi dear members, i have a python list of lists:
L=[[8, 7, 16, 28], [21, 2, 4, 10], [10, 4, 2, 21], [20, 1, 0, 15], [15, 0, 1, 20], [28, 16, 7, 8]]
I want to obtain: [[8, 7, 16, 28], [21, 2, 4, 10], [20, 1, 0, 15]]
how to reverse either of the lists : example: [[21, 2, 4, 10], [10, 4, 2, 21]] in to [[21, 2, 4, 10], [21, 2, 4, 10]]

Finally I can get the unique lists using set().
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

Re: How to remove lists that are the same after reversed?

Postby tnknepp » Thu Mar 14, 2013 8:25 pm

Answered in reverse :D order:

birda wrote:how to reverse either of the lists : example: [[21, 2, 4, 10], [10, 4, 2, 21]] in to [[21, 2, 4, 10], [21, 2, 4, 10]]


Reversing second list:
Code: Select all
L[1] = L[1][::-1]


birda wrote:I want to obtain: [[8, 7, 16, 28], [21, 2, 4, 10], [20, 1, 0, 15]]

I think to do this you will have to implement the solution above recursively. L.reverse will not work, because it just reverses your list of lists.

birda wrote:Finally I can get the unique lists using set().

Yes.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm

Re: How to remove lists that are the same after reversed?

Postby hrs » Thu Mar 14, 2013 10:01 pm

Use reverse()
Code: Select all
>>> a = [2,3,4]
>>> a.reverse()
>>> a
[4, 3, 2]
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: How to remove lists that are the same after reversed?

Postby tnknepp » Fri Mar 15, 2013 11:29 am

hrs wrote:Use reverse()
Code: Select all
>>> a = [2,3,4]
>>> a.reverse()
>>> a
[4, 3, 2]


But reverse() does not work as desired for his nested lists.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm

Re: How to remove lists that are the same after reversed?

Postby Mekire » Fri Mar 15, 2013 2:09 pm

This should do it:
Code: Select all
L=[[8, 7, 16, 28], [21, 2, 4, 10], [10, 4, 2, 21], [20, 1, 0, 15], [15, 0, 1, 20], [28, 16, 7, 8]]

new=[]
for guy in L:
    if guy not in new and guy[::-1] not in new:
        new.append(guy)
print(new)
Result:
Code: Select all
[[8, 7, 16, 28], [21, 2, 4, 10], [20, 1, 0, 15]]

Could probably find a one liner if you try hard enough but this is pretty clear.

tnknepp wrote:
birda wrote:Finally I can get the unique lists using set().

Yes.
Sets can't be made with mutable data types. They would have to be converted to tuples first.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How to remove lists that are the same after reversed?

Postby birda » Fri Mar 15, 2013 4:58 pm

thanks all ;)
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests