我已经从文件tuples中复制了大约 2000 多行内容。现在我需要将其中的每个元素与其余部分中的后续元素进行比较,并且我只需要比较每个元素一次,即如果我采用第一个元素并与列表中的每个元素进行比较,那么我可以在其余部分中丢弃它比较。listtxtlistlist
这是我的比较块代码:
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 lineno1, tup in enumerate(R):
print("")
# 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
我的输出:
line: 1 (20, 12, 40, 42, 45) No. 20 is found in line '7' (20, 17, 30, 33, 43).
line: 1 (20, 12, 40, 42, 45) No. 12 is found in line '5' (8, 12, 24, 28, 44).
line: 1 (20, 12, 40, 42, 45) No. 40 is found in line '2' (40, 21, 40, 42, 49).
line: 1 (20, 12, 40, 42, 45) No. 42 is found in line '2' (40, 21, 40, 42, 49).
line: 1 (20, 12, 40, 42, 45) No. 45 is found in line '11' (14, 24, 30, 40, 45).
line: 2 (40, 21, 40, 42, 49) No. 21 is found in line '10' (20, 21, 23, 43, 48).
line: 3 (6, 19, 22, 36, 48) No. 48 is found in line '10' (20, 21, 23, 43, 48).
line: 4 (2, 5, 20, 24, 33) No. 20 is found in line '9' (17, 18, 20, 35, 39).
line: 6 (3, 15, 29, 30, 37) No. 3 is found in line '8' (3, 15, 16, 29, 42).
line: 6 (3, 15, 29, 30, 37) No. 15 is found in line '8' (3, 15, 16, 29, 42).
line: 7 (20, 17, 30, 33, 43) No. 20 is found in line '10' (20, 21, 23, 43, 48).
line: 7 (20, 17, 30, 33, 43) No. 30 is found in line '11' (14, 24, 30, 40, 45).
我需要帮助格式化输出,在我的输出中,我得到了五行输出,tuple我需要简化它并获得干净的输出。我想tuple在一行中获得每个的输出,因为我有超过 1500 个tuples,大约需要 7500 行。
犯罪嫌疑人X
胡说叔叔
繁华开满天机
芜湖不芜
相关分类