【问题标题】:How to get the index of tuples in a 2D list?如何获取二维列表中元组的索引?
【发布时间】:2021-06-15 05:46:20
【问题描述】:

我需要获取相互链接的元组位置,其中至少有一个元素。

 [('1','1','1'),

  ('X','1','X'),

  ('Z','Z','Z'),

  ('Y','Y','X')]

这里,在第一个元组中,值“1”出现在第二个元组中。 而且,现在在最后一个元组中的第二个元组值“X”。

所以,我需要将所有这些组合到一个列表中,而元组 3 值 ['Z'] 与任何其他元组都不匹配。因此,它存储在单独的列表中。

Expected : [[0,1,3],[2]]

我的做法:

df = [('1','1','1'),('X','1','X'),('Z','Z','Z'),('Y','Y','X')]
res = [list(sub) for sub in df]
res
count=0
list_A=[]
for i,j in enumerate(res): 
    for m,n in enumerate(j):
        if res[i][m] in res[i]:
            print(res.index(j))

【问题讨论】:

  • 在 [('1','2'),{'2', '3'), ('3','4')] 上的预期行为是什么? [0, 1, 2] 似乎不正确,因为 0 和 2 没有任何共同点。你想要 [0, 1] 和 [1, 2]?
  • 0 的元组与 1 有共同的元素,1 的元组在 3 的元组有共同的元素,因此所有这些都是连接/链接的。所以我的列表中的第一个列表应该包含所有链接元组的索引。第二个列表是被遗漏的元组。
  • 您能否阐明您要通过索引处理实现的目标? im 已经是 indizes,jn 已经是元组和值。您的代码知道它处理的元组的索引。 res.index(j) 应该做什么?您是在问您当前的代码有什么问题,或者如何解决出现的问题?
  • 这看起来与拓扑排序有关。您期望重复或循环链接会发生什么?例如。 [('1', '2'), ('2', 'A'), ('2', 'B'), ('A', '4'), ('B', '5')][('1', '2'), ('2', '3'), ('3', '1')] 的预期结果是什么?
  • 对于您给出预期输出的示例,[[0,1,2,3,4]][[0,1,2]]。输入[('1', '2'), ('2', '3'), ('3', '1'), ('4', '5')] 的另一个示例,输出应为[[0,1,2],[3]]。输出是通过至少一个元素链接的元组索引。这里,索引 3 单独在单独的列表中,因为索引 3 元素在其他索引值中没有任何公共元素

标签: python list nested tuples


【解决方案1】:

试试这个代码:

df = [('1', '1', '1'),  # 0
      ('X', '1', 'X'),  # 1
      ('Z', 'Z', 'Z'),  # 2
      ('Y', 'Y', 'X'),  # 3
      ('Z', 'Z', 'Z'),  # 4
      ('2', '2', '2')]  # 5
# first gather all the sublist numbers that intersect
out = []
for x in df:
    out.append({n for n, v in enumerate(df) if set(x).intersection(v)})
# then remove intersected (duplicated) sublist number subsets
output = []
while out:
    buf = out.pop(0)
    for x in out:
        if buf & x:  # intersection not empty
            buf.update(x)
            out.remove(x)
    output.append(list(buf))
print(output)

输出:

[[0, 1, 3], [2, 4], [5]]

【讨论】:

    【解决方案2】:
    def find_linked_tuple_positions(tuple_list):
        tuple_list: list
    
        result_list = []
        checked_tuples = []
        for index, current_tuple in enumerate(tuple_list):
            if index in checked_tuples:
                continue
            linked_tuples = [index]
            result_list.append(linked_tuples)
            current_unique_items = set(current_tuple)
            for next_index, next_tuple in enumerate(tuple_list[index + 1:], index + 1):
                print(next_index, next_tuple)
                next_tuple_unique_items = set(next_tuple)
                match_items = [unique_item for unique_item in current_unique_items if
                               unique_item in next_tuple_unique_items]
                if match_items:  # tuples are linked
                    current_unique_items = current_unique_items | next_tuple_unique_items  # updating set
                    linked_tuples.append(next_index)  # updating the new link
                    checked_tuples.append(next_index)
        return result_list
    
    
    def main():
        tuple_list = [('1', '1', '1'), ('X', '1', 'X'), ('Z', 'Z', 'Z'), ('Y', 'Y', 'X')]
        result_list = find_linked_tuple_positions(tuple_list)
        print(result_list)
    

    【讨论】: