为了账号安全,请及时绑定邮箱和手机立即绑定

简化二维列表比较的输出

简化二维列表比较的输出

aluckdog 2023-06-27 18:34:34
我已经从文件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 行。
查看完整描述

4 回答

?
犯罪嫌疑人X

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)


查看完整回答
反对 回复 2023-06-27
?
胡说叔叔

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}.")


查看完整回答
反对 回复 2023-06-27
?
繁华开满天机

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


查看完整回答
反对 回复 2023-06-27
?
芜湖不芜

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)")



查看完整回答
反对 回复 2023-06-27
  • 4 回答
  • 0 关注
  • 163 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信