[英]How can I optimize the intersections between lists with two elements and generate a list of lists without duplicates in python?
[英]I want to create a program that returns a list that contains only the elements that are common between the lists (without duplicates)
下面是代码:
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 = []
cal = len(a)
for d in range(cal):
if a[d] in b and a[d] not in c:
c.append(a[d])
print(c)
以下是输出:
[1、2、3、5、8、13]
流程结束,退出代码为0
我自己无法做出逻辑,因此我查看了解决方案,但仍然没有在代码背后找到概念。 我感到困惑的一点是,在cal中,我存储的a的长度是11,然后我执行了11次for循环(我不确定为什么这样做,以及我们将从这部分得到什么) ,然后最终使用“ a [d]”作为条件。 这对我来说没有任何意义。
你甚至可以用一套
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 = filter(set(a).__contains__, b)
print(list(c))
# [1, 2, 3, 5, 8, 13]
十分简单...
c = [i for i in set(a) if i in b]
set(a)
调用消除了第一个列表中的所有重复项,从而解决了整个答案。 这使用了所谓的“列表理解”,它使代码非常简洁但功能强大。
让我们将想法扩展为英语:
for each of the 11 positions in a:
grab the element in that position ...
if that element is also in b (so it's in both a and b),
but is not yet in c (so we get no duplicates):
add it to c
这是否为您清除了逻辑?
首先,如果您打算更改这些列表,那么此代码在逻辑上是不正确的。
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 = []
cal = min(len(a),len(b))
for d in range(cal):
if a[d] in b and a[d] not in c:
c.append(a[d])
print(c)
我们正在for循环中检查索引d的元素是否具有从0到11的每个值,如果a [d]的元素(在a中的第d个元素)也位于b中。 如果它在b中,并且尚未添加到列表c中,则我们将其添加。
一个人可以用很多语法上更好的方式做到这一点:
c = list(set([d for a in d if d in b]))
要么
c = filter(set(a).__contains__, b)
要么
c = list(set(a).union(b))
让我们打破代码
range(integer_value)
返回从0到integer_value -1的元素列表,这就是为什么您可以对其进行迭代的原因
这样说:
#this is returning the longitude of the list
cal = len(a)
#this is iterating over a list of [0,1,2,3... until len -1)
for d in range(cal):
#this checks if the value of a in the position d is in the list called b
#this is also checking that it's not in c to avoid duplication,
#but you could use a set which is a "list" that doesn't accept duplicates
if a[d] in b and a[d] not in c:
c.append(a[d])
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 = []
c = list( set(a).intersection(set(b)) )
print (c)
鉴于您具有集合的基本知识,因此该代码是不言自明的。
您正在做的是将列表a与列表b的元素进行比较。
仅当列表a的元素也位于列表b中时,才将其放入列表c。
列表c将包含列表a和b的所有匹配数字,显然是这种情况。
关于使您感到困惑的比较:
您必须循环11次,因为列表a的len为11。如果要将列表a的每个元素与列表b进行比较,则必须这样做。
for d in range(cal):
表示您获得的数字范围是从0到cal的值(这是列表a的长度),在这种情况下为0,1,2,... 10。
a[d]
然后意味着
a[0]
a[1]
...
a[10]
因此您最终遍历了列表的每个元素。
希望能帮助您的理解。
最好,
我
import random
my_list = random.sample(range(1,30), 10)
my_list.sort()
my_list2 = random.sample(range(1,40), 15)
my_list2.sort()
print(my_list)
print()
print(my_list2)
print()
common = []
for i in my_list:
if i in my_list2:
common.append(i)
print(common)
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 for x in set(a) if x in set(b)]
print c
输出:
[1, 2, 3, 5, 8, 13]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.