Python 2.7 为什么map函数没有';是否返回没有重复元素的列表?

Python 2.7 为什么map函数没有';是否返回没有重复元素的列表?,python-2.7,list,python-3.4,python-3.x,Python 2.7,List,Python 3.4,Python 3.x,我有以下清单: list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7] 我想消除重复的值。map功能的代码取自。 这是完整的测试代码: 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 lis

我有以下清单:

list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
我想消除重复的值。
map
功能的代码取自。 这是完整的测试代码:

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中,是:

在Python 3.4中,它打印:

为什么Python3中的
map
函数返回空列表

因为在
地图中
而不是立即评估。它就像一个生成器,可以根据需要动态生成元素:这可能更有效,因为您可能只需要前三个元素,所以为什么要计算所有元素?因此,只要您不以某种方式具体化
map
的输出,您就没有真正计算map

例如,您可以使用
list(…)
强制Python对列表求值:

list(map(lambda x: not x in list2 and list2.append(x), list1))
list(映射(lambda x:not x in list2和list2.append(x),list1))

在这种情况下,将为
list2

生成相同的结果。为什么不使用
np.unique
?因为这不是生产代码,这是为了学习:)
[]
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
list(map(lambda x: not x in list2 and list2.append(x), list1))