python - How do you clean up redundant values in a list of lists -


say have list of list x=[[0,0,0,3,4],[8,8,9,2,8,2]]

how make each sublist contain repeated number once:
new list:

xnew=[[0,3,4],[8,9,2]]

you can use set that:

new_x = [list(set(i)) in old_x] 

sets collection of unique elements , therefore create set of unique values when list of duplicate values cast set. can convert set list , desired result.

example

>>> old_x = [[0,0,0,3,4],[8,8,9,2,8,2]] >>> new_x = [list(set(i)) in old_x] >>> print new_x [[0,3,4],[8,9,2]] 

Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -