猿问

简化二维列表比较的输出

我已经从文件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 行。


aluckdog
浏览 164回答 4
4回答

犯罪嫌疑人X

只是很多循环:) 请注意,某些行与另一行有多个匹配。我把它们留在里面了。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)

胡说叔叔

我不确定我的问题是否正确,特别是因为我认为你已经快要问了。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}.")

繁华开满天机

三个嵌套循环似乎效率不高,但对我来说这是最明显的解决方案。对于列表中的每个元组,迭代元组中的每个数字,并将其与列表其余部分中每个元组中相同位置的数字进行比较,直到找到匹配项。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

芜湖不芜

如果我正确理解了这个问题,你只需要迭代一堆......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)")
随时随地看视频慕课网APP

相关分类

Python
我要回答