如何获取二维列表中元组的索引?
How to get the index of tuples in a 2D list?
我需要获取相互链接的元组位置,其中至少有一个元素。
[('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))
试试这个代码:
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]]
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)
我需要获取相互链接的元组位置,其中至少有一个元素。
[('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))
试试这个代码:
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]]
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)