Python循环遍历列表中的特定元素
我以以下方式列出了两个列表,并试图以某种特定的方式对它们进行比较:Python循环遍历列表中的特定元素,python,for-loop,Python,For Loop,我以以下方式列出了两个列表,并试图以某种特定的方式对它们进行比较: a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]] b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]] 我想要什么 我想按以下顺序比较a和b的每个元素 例如,在上述情况下,在a的第一个元素[1
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
我想要什么
我想按以下顺序比较a
和b
的每个元素
例如,在上述情况下,在a
的第一个元素[18,299,559]
等于[18,299,559]
的b
的第一个元素[18,299,559]
之后,循环将中断,在新的循环中a
的第二个元素,即[18,323,564]
将从的[18,323,564]
开始进行比较
这里的要点是,在将a
的所有元素分别与第0个索引中的b
的所有元素进行比较时,新循环不应再次从b
的第一个元素开始迭代
p.S.在本例中,我不想检查两个列表中是否存在元素,也不想通过使用set
方法轻松找到任何缺失的元素。这个逻辑只是我自己的知识
我尝试的
所有普通嵌套循环方法如下所示:
for i in a:
for j in b: #after break it would always start with first element in b
if i == j:
break
这里的问题是在每次中断后
新的i
与b
的第一个元素进行比较,而不是与a
匹配的最后一个元素进行比较
我脑海中浮现的东西是这样的:
for i in a:
for j in b:
if i == j:
print something
save index of j
break
in the next loop start comparing new i from this saved index of j
然而,我无法将这个想法转化为代码
我知道这听起来很荒谬,但就循环而言,这样的东西可以在python中实现吗?欢迎提出所有意见和提示 当然,您可以追踪您在b
中取得的进展。我将调用这个变量start\u j
,从这里开始,它可以在b
中开始比较:
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
start_j = 0
for i in range(len(a)):
for j in range(start_j, len(b)):
if a[i] == b[j]:
print 'match found:', i, j
start_j = j
break
如果找到了匹配的元素,start_j
被设置为最新的j
,下一次内部循环(用于检查b
中的元素)从那里开始
输出:
match found: 0 1
match found: 1 2
match found: 3 3
match found: 4 4
当然,您可以追踪您在b
中所取得的进展。我将调用这个变量start\u j
,从这里开始,它可以在b
中开始比较:
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
start_j = 0
for i in range(len(a)):
for j in range(start_j, len(b)):
if a[i] == b[j]:
print 'match found:', i, j
start_j = j
break
如果找到了匹配的元素,start_j
被设置为最新的j
,下一次内部循环(用于检查b
中的元素)从那里开始
输出:
match found: 0 1
match found: 1 2
match found: 3 3
match found: 4 4
当然,您可以追踪您在b
中所取得的进展。我将调用这个变量start\u j
,从这里开始,它可以在b
中开始比较:
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
start_j = 0
for i in range(len(a)):
for j in range(start_j, len(b)):
if a[i] == b[j]:
print 'match found:', i, j
start_j = j
break
如果找到了匹配的元素,start_j
被设置为最新的j
,下一次内部循环(用于检查b
中的元素)从那里开始
输出:
match found: 0 1
match found: 1 2
match found: 3 3
match found: 4 4
当然,您可以追踪您在b
中所取得的进展。我将调用这个变量start\u j
,从这里开始,它可以在b
中开始比较:
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
start_j = 0
for i in range(len(a)):
for j in range(start_j, len(b)):
if a[i] == b[j]:
print 'match found:', i, j
start_j = j
break
如果找到了匹配的元素,start_j
被设置为最新的j
,下一次内部循环(用于检查b
中的元素)从那里开始
输出:
match found: 0 1
match found: 1 2
match found: 3 3
match found: 4 4
根据您的描述:
另外,在这个例子中,我并没有试图检查两个列表中是否存在元素,也没有试图找到任何缺失的元素,这可以通过使用set方法轻松完成。这个逻辑只是我自己的知识
我想你是在尝试对这两个列表进行一系列比较。为什么不把它们变成一套呢
aset = set(tuple(item) for item in a)
bset = set(tuple(item) for item in b)
print 'Items only in A:', aset - bset
print 'Items only in B:', bset - aset
print 'Items in common:', aset & bset
注意:我必须将每个子列表转换为元组,因为列表是不可散列的,因此不能在一个集中。根据您的描述:
另外,在这个例子中,我并没有试图检查两个列表中是否存在元素,也没有试图找到任何缺失的元素,这可以通过使用set方法轻松完成。这个逻辑只是我自己的知识
我想你是在尝试对这两个列表进行一系列比较。为什么不把它们变成一套呢
aset = set(tuple(item) for item in a)
bset = set(tuple(item) for item in b)
print 'Items only in A:', aset - bset
print 'Items only in B:', bset - aset
print 'Items in common:', aset & bset
注意:我必须将每个子列表转换为元组,因为列表是不可散列的,因此不能在一个集中。根据您的描述:
另外,在这个例子中,我并没有试图检查两个列表中是否存在元素,也没有试图找到任何缺失的元素,这可以通过使用set方法轻松完成。这个逻辑只是我自己的知识
我想你是在尝试对这两个列表进行一系列比较。为什么不把它们变成一套呢
aset = set(tuple(item) for item in a)
bset = set(tuple(item) for item in b)
print 'Items only in A:', aset - bset
print 'Items only in B:', bset - aset
print 'Items in common:', aset & bset
注意:我必须将每个子列表转换为元组,因为列表是不可散列的,因此不能在一个集中。根据您的描述:
另外,在这个例子中,我并没有试图检查两个列表中是否存在元素,也没有试图找到任何缺失的元素,这可以通过使用set方法轻松完成。这个逻辑只是我自己的知识
我想你是在尝试对这两个列表进行一系列比较。为什么不把它们变成一套呢
aset = set(tuple(item) for item in a)
bset = set(tuple(item) for item in b)
print 'Items only in A:', aset - bset
print 'Items only in B:', bset - aset
print 'Items in common:', aset & bset
注意:我必须将每个子列表转换为一个元组,因为列表是不可散列的,因此不能在一个集合中。这样如何:
start_idx = 0
for i in a:
for j_idx, j in enumerate(b[start_idx:], start=start_idx):
if j==a:
print 'Bingo!'
start_idx = j_idx
break
p.S范围(len(a))-很难看:)这个怎么样:
start_idx = 0
for i in a:
for j_idx, j in enumerate(b[start_idx:], start=start_idx):
if j==a:
print 'Bingo!'
start_idx = j_idx
break
p.S范围(len(a))-很难看:)这个怎么样:
start_idx = 0
for i in a:
for j_idx, j in enumerate(b[start_idx:], start=start_idx):
if j==a:
print 'Bingo!'
start_idx = j_idx
break
p.S范围(len(a))-很难看:)这个怎么样:
start_idx = 0
for i in a:
for j_idx, j in enumerate(b[start_idx:], start=start_idx):
if j==a:
print 'Bingo!'
start_idx = j_idx
break
p.S范围(len(a))-它很难看:)以下是如何在不使用continue或bick的情况下完成它
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
matches = []
for i in xrange(len(a)):
for j in xrange(len(b)):
if a[i] == b[j]:
matches.append([i,j])
for match in matches: print(match)
下面是如何在没有继续或喙的情况下完成它
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
matches = []
for i in xrange(len(a)):
for j in xrange(len(b)):
if a[i] == b[j]:
matches.append([i,j])
for match in matches: print(match)
下面是如何在没有继续或喙的情况下完成它
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
matches = []
for i in xrange(len(a)):
for j in xrange(len(b)):
if a[i] == b[j]:
matches.append([i,j])
for match in matches: print(match)
下面是如何在没有继续或喙的情况下完成它
a = [[18, 299, 559], [18, 323, 564], [18, 353, 564], [18, 391, 570], [18, 448, 570]]
b = [[18, 353, 564], [18, 299, 559], [18, 323, 564], [18, 391, 570], [18, 448, 570]]
matches = []
for i in xrange(len(a)):
for j in xrange(len(b)):
if a[i] == b[j]:
matches.append([i,j])
for match in matches: print(match)
您是否尝试过继续
而不是中断
?@lanAuld没有。让我喝一杯look@LanAuld尝试将上述代码中的break
替换为continue
,但仍然没有得到我想要的。你能强调一下我应该如何使用它吗?@MathiasEttinger使用zip两个循环将以并行方式迭代。这将只是逐行比较两个元素。您是否尝试过继续而不是中断?@lanAuld不,我没有。