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
Post a Comment