本文介绍了包含特定元组的元组索引列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 list1 的 3 个元组子列表的列表,例如

I have a list list1 of 3 sublists of tuples like

[[(['A', 'B', 'A'], ['B', 'O', 'A']),
  (['A', 'B', 'A'], ['B', 'A', 'O']),
  (['A', 'B', 'O'], ['B', 'O', 'A']),
  (['A', 'B', 'O'], ['B', 'A', 'O']),
  (['A', 'B', 'A'], ['B', 'O', 'A']),
  (['A', 'B', 'A'], ['B', 'A', 'O'])],
 [(['A', 'B', 'A'], ['B', 'A', 'A']),
  (['A', 'B', 'O'], ['B', 'A', 'A']),
  (['A', 'B', 'A'], ['B', 'A', 'A'])],
 [['A', 'B', 'A'], ['A', 'B', 'O']],
 [['A', 'B', 'B']],
 [['B', 'A', 'A']]]

假设 list2 = ['A', 'B', 'A']. 我的目标是获得任何元组对的索引列表(或list1 中包含元组 list2 的一组单例元组.我尝试使用 enumerate 函数如下但结果不正确

Assume list2 = ['A', 'B', 'A']. My goal is to obtain a list of indices of any pairs of tuples (or a singleton set of tuple) in list1 that contain the tuple list2. I tried to use the enumerate function as follows but the result is not correct

print([i for i, j in enumerate(bigset) if ['A', 'B', 'A'] in j[0] or 
       ['A', 'B', 'A'] == j[0] or [['A', 'B', 'A']] in j[0]])

谁能帮我解决这个问题?由于 list1 中出现的元组元组的不同大小不匹配,我陷入了困境.

Can anyone please help me with this problem? I'm quite stuck due to the mismatch in the different sizes of tuples of tuples appearing in list1.

我的另一个问题是:我想在 list1 中找到 3 元素列表的总数.所以如果我手工做,答案是22.但是如何在代码中做到这一点?我想我们需要使用两个 for 循环?

Another question I have is: I want to find the total number of 3-element lists in list1. So if I do it by hand, the answer is 22. But how to do it in code? I guess we need to use two for loops?

预期输出 对于上面带有给定 list2list1,我们将获得包含 list2 的索引列表是 [0,1,5,6,7,9,10].

Expected Output For list1 above with the given list2, we would get the list of indices containing list2 is [0,1,5,6,7,9,10].

推荐答案

好的,开始

这使用递归,因为我们不知道你的 list1 的深度,所以索引将被这样计算:

This use recursion because we don't know the depth of your list1 SO the index will be counted like this :

0,1
2,3,4,
6,7
8,
9,10,11,12

等...(与您将其写在 1 行中的顺序相同)

etc... (The same order you have by writing it in 1 row)

这里的结果是:

[0, 2, 8, 10, 12, 16, 18]

现在是代码

def foo(l,ref):
    global s
    global indexes
    for items in l:  #if it's an element of 3 letters
        if len(items)== 3 and len(items[0])==1:
            if items == ref: 
                indexes.append(s) #save his index if it match the ref
            s+= 1  #next index
        else: #We need to go deeper
            foo(items,ref)
    return(s)
          
        
list1 = [[(['A', 'B', 'A'], ['B', 'O', 'A']),
  (['A', 'B', 'A'], ['B', 'A', 'O']),
  (['A', 'B', 'O'], ['B', 'O', 'A']),
  (['A', 'B', 'O'], ['B', 'A', 'O']),
  (['A', 'B', 'A'], ['B', 'O', 'A']),
  (['A', 'B', 'A'], ['B', 'A', 'O'])],
 [(['A', 'B', 'A'], ['B', 'A', 'A']),
  (['A', 'B', 'O'], ['B', 'A', 'A']),
  (['A', 'B', 'A'], ['B', 'A', 'A'])],
 [['A', 'B', 'A'], ['A', 'B', 'O']],
 [['A', 'B', 'B']],
 [['B', 'A', 'A']]]

list2 = ['A', 'B', 'A']
indexes = []
s=0
count= foo(list1,list2)
print(indexes)

s 是我们正在处理的索引count 是元素的总数(22).Indexes 是你想要的索引列表.

s is the index we are working oncount is the total amount of element (22).Indexes is the list of index you want.

即使你做了一个 list3 = [list1,list1,[list1,[list1],list1]] ,你也可以尝试一下.

This work even if you make a list3 = [list1,list1,[list1,[list1],list1]] , you may want to try it.

祝你好运现在​​结束你的脚本.

Best luck to end your script now.

这篇关于包含特定元组的元组索引列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:47