Several methods of python list deduplication (array)
1. Method 1
code show as below | copy code |
ids = [1,2,3,3,4,2,3,4,5,6,1] |
The ideas seem relatively clear and simple, and the previous order can also be maintained.
2. Method 2
Processing through the set method
code show as below | copy code |
ids = [1,4,3,3,4,2,3,4,5,6,1] |
It is relatively simple to process, using the collection method set for processing, but the results will not retain the previous order.
3. Method 3
Processing using lambda anonymous functions and reduce functions
code show as below | copy code |
ids = [1,4,3,3,4,2,3,4,5,6,1] func = lambda x,y:x if y in x else x + [y] reduce(func, [[], ] + ids) |
4. Method 4
Using the itertools module
code show as below | copy code |
import itertools |
5. Unable to maintain the original order
code show as below | copy code |
liebiao=set(liebiao) |
Six, while traversal to remove duplication
code show as below | copy code |
def delRepeat(liebiao): |
1. Method 1
code show as below | copy code |
ids = [1,2,3,3,4,2,3,4,5,6,1] |
The ideas seem relatively clear and simple, and the previous order can also be maintained.
2. Method 2
Processing through the set method
code show as below | copy code |
ids = [1,4,3,3,4,2,3,4,5,6,1] |
It is relatively simple to process, using the collection method set for processing, but the results will not retain the previous order.
3. Method 3
Processing using lambda anonymous functions and reduce functions
code show as below | copy code |
ids = [1,4,3,3,4,2,3,4,5,6,1] func = lambda x,y:x if y in x else x + [y] reduce(func, [[], ] + ids) |
4. Method 4
Using the itertools module
code show as below | copy code |
import itertools |
五、无法保持原有顺序
代码如下 | 复制代码 |
liebiao=set(liebiao) |
六、while遍历去重
代码如下 | 复制代码 |
def delRepeat(liebiao): |