4 回答
TA贡献2080条经验 获得超4个赞
只是很多循环:) 请注意,某些行与另一行有多个匹配。我把它们留在里面了。
R = [(20, 12, 40, 42, 45),
(40, 21, 40, 42, 49),
(6, 19, 22, 36, 48),
(2, 5, 20, 24, 33),
(8, 12, 24, 28, 44),
(3, 15, 29, 30, 37),
(20, 17, 30, 33, 43),
(3, 15, 16, 29, 42),
(17, 18, 20, 35, 39),
(20, 21, 23, 43, 48),
(14, 24, 30, 40, 45)]
for i,x in enumerate(R):
for ie, e in enumerate(x):
for rw in range(i+1, len(R)):
if e == R[rw][ie]:
print(f"In line: {i+1} {x} No. '{e}' is found in line {rw+1} {R[rw]}")
break
输出
In line: 0 (20, 12, 40, 42, 45) No. '20' is found in line 6 (20, 17, 30, 33, 43)
In line: 0 (20, 12, 40, 42, 45) No. '12' is found in line 4 (8, 12, 24, 28, 44)
In line: 0 (20, 12, 40, 42, 45) No. '40' is found in line 1 (40, 21, 40, 42, 49)
In line: 0 (20, 12, 40, 42, 45) No. '42' is found in line 1 (40, 21, 40, 42, 49)
In line: 0 (20, 12, 40, 42, 45) No. '45' is found in line 10 (14, 24, 30, 40, 45)
In line: 1 (40, 21, 40, 42, 49) No. '21' is found in line 9 (20, 21, 23, 43, 48)
In line: 2 (6, 19, 22, 36, 48) No. '48' is found in line 9 (20, 21, 23, 43, 48)
In line: 3 (2, 5, 20, 24, 33) No. '20' is found in line 8 (17, 18, 20, 35, 39)
In line: 5 (3, 15, 29, 30, 37) No. '3' is found in line 7 (3, 15, 16, 29, 42)
In line: 5 (3, 15, 29, 30, 37) No. '15' is found in line 7 (3, 15, 16, 29, 42)
In line: 6 (20, 17, 30, 33, 43) No. '20' is found in line 9 (20, 21, 23, 43, 48)
In line: 6 (20, 17, 30, 33, 43) No. '30' is found in line 10 (14, 24, 30, 40, 45)
TA贡献1804条经验 获得超8个赞
我不确定我的问题是否正确,特别是因为我认为你已经快要问了。
R = [(20, 12, 40, 42, 45), (40, 21, 40, 42, 49),
(6, 19, 22, 36, 48), (2, 5, 20, 24, 33),
(8, 12, 24, 28, 44), (3, 15, 29, 30, 37),
(20, 17, 30, 33, 43), (3, 15, 16, 29, 42),
(17, 18, 20, 35, 39), (20, 21, 23, 43, 48),
(14, 24, 30, 40, 45),]
for i,x in enumerate(R):
a = set(x)
for j in range(i+1,len(R)):
y = R[j]
b = set(y)
ab = a&b
for n in ab:
print(f"In line: {i+1} {x} No. '{n}' is found in line {j+1} {y}.")
TA贡献1816条经验 获得超4个赞
三个嵌套循环似乎效率不高,但对我来说这是最明显的解决方案。
对于列表中的每个元组,迭代元组中的每个数字,并将其与列表其余部分中每个元组中相同位置的数字进行比较,直到找到匹配项。
for lineno1, tup in enumerate(R):
# iterate over the current tuple
for i, num in enumerate(tup):
# compare every number in the tuple to the rest of the list
for lineno2 in range(lineno1+1, len(R)):
tup2 = R[lineno2]
if num == tup2[i]:
print(f"In line: {lineno1+1} {tup} No. '{num} is found in line {lineno2+1} {tup2}.")
break
TA贡献1796条经验 获得超7个赞
如果我正确理解了这个问题,你只需要迭代一堆......
R = [(20, 12, 40, 42, 45), (40, 21, 40, 42, 49),
(6, 19, 22, 36, 48), (2, 5, 20, 24, 33),
(8, 12, 24, 28, 44), (3, 15, 29, 30, 37),
(20, 17, 30, 33, 43), (3, 15, 16, 29, 42),
(17, 18, 20, 35, 39), (20, 21, 23, 43, 48),
(14, 24, 30, 40, 45)...]
for i in range(len(R)):
list_a = R[i]
for d in range(len(list_a)):
val_to_find = list_a[d]
for j in range((i+1), len(R)):
list_b = R[j]
for k in range(list_b):
cur_val = list_b[k]
if(val_to_find == cur_val):
print("In line: " + string(i) + " " + string(list_a) + " No. " + string(list_a[d]) + " is found in line " + string(k) + " " + string(list_b)")
添加回答
举报