Python列表去掉重复元素的一种方法:

>>> L = [1, 2, 3, 4, 1, 2, 3, 4, 5]
>>> [x for x in L if x not in locals()['_[1]']]
[1, 2, 3, 4, 5]

解释如下:

down vote Referencing a list comprehension as it is being built...
You can reference a list comprehension as it is being built by the symbol '_[1]'. For example, the following function unique-ifies a list of elements without changing their order by referencing its list comprehension.

def unique(my_list):
    return [x for x in my_list if x not in locals()['_[1]']]

相关文章:

  • 2021-10-29
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
猜你喜欢
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案