Python 列表理解逻辑不工作和I';我不知道为什么

Python 列表理解逻辑不工作和I';我不知道为什么,python,list-comprehension,Python,List Comprehension,我正在做的练习要求我创建并打印一个列表,其中包含以下两个列表中的所有公共元素,且不重复: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 我试图用一行代码创建一个新列表,我认为我的逻辑是正确的,但显然它在某个地方有问题 以下是目前不起作用的内容: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3

我正在做的练习要求我创建并打印一个列表,其中包含以下两个列表中的所有公共元素,且不重复:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] 

b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
我试图用一行代码创建一个新列表,我认为我的逻辑是正确的,但显然它在某个地方有问题

以下是目前不起作用的内容:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

common_list = []

common_list = [nums for nums in a if (nums in b and nums not in common_list)]

print(common_list)
我希望得到
[1,2,3,5,8,13]
,但是1仍然是重复的,即使我有“nums not in common_list”的情况,所以我最终得到了
[1,1,2,3,5,8,13]

我建议您使用集合来避免重复值

common_set = set()
您可以通过以下方式添加项目:

common_set.add(value)
最后,您可以通过以下方式打印值:

print(common_set)

我建议您使用集合来避免重复值,而不是使用列表

common_set = set()
您可以通过以下方式添加项目:

common_set.add(value)
最后,您可以通过以下方式打印值:

print(common_set)
您可以使用枚举:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]


res = [i for n, i in enumerate(a) if i not in a[:n] and i in b]
print (res)
输出:

[1, 2, 3, 5, 8, 13]
您可以使用枚举:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]


res = [i for n, i in enumerate(a) if i not in a[:n] and i in b]
print (res)
输出:

[1, 2, 3, 5, 8, 13]

使用列表执行此操作的一种方法是(假设其中一个列表没有重复项):

a=[1,1,2,3,5,8,13,21,34,55,89]
b=[1,2,3,4,5,6,7,8,9,10,11,12,13]
c=[x代表a中的x,如果x代表b]
印刷品(c)
# [1, 2, 3, 5, 8, 13]
或者,对于任何列表:


a=[1,1,2,3,5,8,13,21,34,55,89]
b=[1,2,3,4,5,6,7,8,9,10,11,12,13]
c=[]
对于a+b中的x:
如果x在a中,x在b中,x不在c中:
c、 附加(x)
印刷品(c)
# [1, 2, 3, 5, 8, 13]
但是
set
s更适合于此:

a={1,1,2,3,5,8,13,21,34,55,89}
b={1,2,3,4,5,6,7,8,9,10,11,12,13}
c=a.交叉口(b)
印刷品(c)
# {1, 2, 3, 5, 8, 13}

处理列表的一种方法是(假设其中一个列表没有重复项):

a=[1,1,2,3,5,8,13,21,34,55,89]
b=[1,2,3,4,5,6,7,8,9,10,11,12,13]
c=[x代表a中的x,如果x代表b]
印刷品(c)
# [1, 2, 3, 5, 8, 13]
或者,对于任何列表:


a=[1,1,2,3,5,8,13,21,34,55,89]
b=[1,2,3,4,5,6,7,8,9,10,11,12,13]
c=[]
对于a+b中的x:
如果x在a中,x在b中,x不在c中:
c、 附加(x)
印刷品(c)
# [1, 2, 3, 5, 8, 13]
但是
set
s更适合于此:

a={1,1,2,3,5,8,13,21,34,55,89}
b={1,2,3,4,5,6,7,8,9,10,11,12,13}
c=a.交叉口(b)
印刷品(c)
# {1, 2, 3, 5, 8, 13}

如其他答案和评论中所述,您的问题是,在列表理解过程中,
公共列表
为空

现在来看实际解决方案:如果顺序不重要,
set
是您的朋友:

common_list = list(set(a) & set(b))
seen = set()
bset = set(b) # makes `in` test much faster
common_list = []

for item in a:
    if item in seen:
        continue
    if item in bset:
        common_list.append(item)
        seen.add(item)
如果顺序很重要,
集合仍然是你的朋友:

common_list = list(set(a) & set(b))
seen = set()
bset = set(b) # makes `in` test much faster
common_list = []

for item in a:
    if item in seen:
        continue
    if item in bset:
        common_list.append(item)
        seen.add(item)

如其他答案和评论中所述,您的问题是,在列表理解过程中,
common_list
为空

现在来看实际解决方案:如果顺序不重要,
set
是您的朋友:

common_list = list(set(a) & set(b))
seen = set()
bset = set(b) # makes `in` test much faster
common_list = []

for item in a:
    if item in seen:
        continue
    if item in bset:
        common_list.append(item)
        seen.add(item)
如果顺序很重要,
集合仍然是你的朋友:

common_list = list(set(a) & set(b))
seen = set()
bset = set(b) # makes `in` test much faster
common_list = []

for item in a:
    if item in seen:
        continue
    if item in bset:
        common_list.append(item)
        seen.add(item)
一艘班轮:

list(set(a).intersection(b))
一艘班轮:

list(set(a).intersection(b))

请看一看python
common_list
仅在计算完整个列表理解后创建。列表理解中
common_list
的值仅为
[]
。只有在理解完成后,新的结果列表才会分配给
公共列表
。可能的重复项。看起来这个人和你有同样的问题。另一个可能的重复:看看python
common\u list
,它只是在整个列表理解完成后创建的。列表理解中
common\u list
的值只是
[]
。只有在理解完成后,新的结果列表才会分配给
公共列表
。可能的重复项。看起来这个人和你有同样的问题。另一个可能的欺骗: