【发布时间】:2017-02-04 17:02:57
【问题描述】:
我有这个清单:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
我想摆脱重复值。 map 函数的代码取自 here。
这是完整的测试代码:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
list2 = []
map(lambda x: not x in list2 and list2.append(x), list1)
print(list2)
list2 = []
[list2.append(c) for c in list1 if c not in list2]
print(list2)
list2 = []
for c in list1:
if c not in list2:
list2.append(c)
print(list2)
在 Python 2.7 中是打印:
[1, 3, 56, 6, 7] [1, 3, 56, 6, 7] [1, 3, 56, 6, 7]
在 Python 3.4 中,它会打印:
[] [1, 3, 56, 6, 7] [1, 3, 56, 6, 7]
为什么map 函数在 Python3 中返回一个空列表?
【问题讨论】:
-
你为什么不用
np.unique? -
因为这不是生产代码,所以仅供学习使用:)
标签: python-2.7 list python-3.4