Python : Remove duplicate elements in lists and sublists; and remove full sublist if duplicate -
is there function or way achieve recursively in python 2.7 ?
input : ['and', ['or', 'p', '-r', 'p'], ['or', '-q', '-r', 'p']] output : ['and', ['or', 'p', '-r'], ['or', '-q', '-r', 'p']]
remove duplicate 'p' in sublist1 duplicate
input : ['and', ['or', 'p', '-r', 'p'], ['or', '-q', '-r', 'p'], ['or', 'p', '-r', 'p']] output : ['and', ['or', 'p', '-r'], ['or', '-q', '-r', 'p']]
remove duplicate 'p' in sublist1 duplicate remove sublist3 duplicate of sublist1
thanks
i think have create custom remove duplicate
function inorder preserve order of sublists.try this:
def rem_dup(lis): y, s = [], set() t in lis: w = tuple(sorted(t)) if isinstance(t, list) else t if not w in s: y.append(t) s.add(w) return y inp = ['and', ['or', 'p', '-r', 'p'], ['or', '-q', '-r', 'p'], ['or', 'p', '-r', 'p']] out = [rem_dup(i) if isinstance(i, list) else in rem_dup(inp)] >>>out ['and', ['or', 'p', '-r'], ['or', '-q', '-r', 'p']]
Comments
Post a Comment