list去掉重复元素
需求: 有list 里面含有重复元素,要求去掉重复元素:
solution 1:
>>> a [1, 2, 2, 1, 3, 4, 5, 6, 5] >>> set(a) set([1, 2, 3, 4, 5, 6]) >>> list(set(a)) [1, 2, 3, 4, 5, 6]
set操作是 Build an unordered collection of unique elements.
solution 2:
>>> a [1, 2, 2, 1, 3, 4, 5, 6, 5] >>> b={} >>> b.fromkeys(a) {1: None, 2: None, 3: None, 4: None, 5: None, 6: None} >>> b.fromkeys(a).keys() [1, 2, 3, 4, 5, 6]
b.fromkeys(S,[v])是以 s序列中的元素为key v中的元素为value生成字典。 v默认为none