发布于 5年前
Python中,列表要怎么去重?
使用 set
,例如:
In [1]: a = [i for i in range(10)] * 2
In [2]: a
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]: [i for i in set(a)]
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
使用 set
,例如:
In [1]: a = [i for i in range(10)] * 2
In [2]: a
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]: [i for i in set(a)]
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]