简体   繁体   English

包含某些元组的元组的元组索引列表

[英]List of indices of tuples of tuples that contain certain tuples

I have a list list1 of 3 sublists of tuples like我有一个包含 3 个元组子列表的列表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']]]

Assume list2 = ['A', 'B', 'A'].假设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 .我的目标是获取包含元组list2 list1中任何一对元组(或一组元组)的索引列表 I tried to use the enumerate function as follows but the result is not correct我尝试使用enumerate函数如下但结果不正确

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

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出现的元组元组的不同大小不匹配,我陷入了困境。

Another question I have is: I want to find the total number of 3-element lists in list1 .我的另一个问题是:我想在list1找到3 元素列表总数 So if I do it by hand, the answer is 22 .所以如果我手工做,答案是22 But how to do it in code?但是如何在代码中做到这一点? I guess we need to use two for loops?我想我们需要使用两个for循环?

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] .预期输出对于上面给定的list2 list1 ,我们将得到包含list2的索引列表是[0,1,5,6,7,9,10]

Would it work for your implementation if we sort out your list1 into a more friendly format first?如果我们首先将您的 list1 整理成更友好的格式,它对您的实施有用吗? If so, you could do that in a pretty simple way:如果是这样,你可以用一种非常简单的方式来做到这一点:

Go through each element of list1, if the element is itself a big list of tuples, then we want to unpack further.遍历list1的每个元素,如果元素本身就是一个大的元组列表,那么我们要进一步解包。 If the element is a tuple (so the first element of that tuple is a list), or it is itself one of your 3-element lists, then we just want to append that as it is.如果元素是一个元组(所以该元组的第一个元素是一个列表),或者它本身就是您的 3 元素列表之一,那么我们只想按原样附加它。

nice_list = []
for i in list1:
    if type(i[0]) == str or type(i[0]) == list:
        # i.e. i is one of your 3-element lists, or a tuple of lists
        nice_list.append(i)
    else:
        #If i is still a big list of other tuples, we want to unpack further
        for j in i:
            nice_list.append(j)

Then you could search for the indices much easier:然后你可以更容易地搜索索引:

for i, idx in zip(nice_list, range(len(nice_list))): 
    if ['A', 'B', 'A'] in i: 
        print(idx) #Or append them to a list, whatever you wanted to do

For a not-particularly-elegant solution to your question about finding how many 3-element lists there are, yes you could use a for loop:对于有关查找有多少个 3 元素列表的问题的非特别优雅的解决方案,是的,您可以使用 for 循环:

no_of_lists = 0
for n in nice_list:
    if type(n) == tuple:
        no_of_lists += len(n)
    elif type(n) == list and type(n[0]) == list:
        # if it is a list of lists
        no_of_lists += len(n)
    elif type(n) == list and type(n[0]) == str:
        #if it is a 3-element list
        no_of_lists_lists += 1
print('Number of 3-element lists contained:', no_of_lists)

Edit: to answer the question you asked in the comments about how the for n in nice_list part works, this just iterates through each element of the list.编辑:要回答您在关于for n in nice_list部分中的for n in nice_list如何工作的评论中提出的问题,这只是遍历列表的每个元素。 To explore this, try writing some code to print out nice_list[0] , nice_list[1] etc, or a for loop which prints out each n so you can see what that looks like.要探索这一点,请尝试编写一些代码来打印nice_list[0]nice_list[1]等,或者使用 for 循环打印出每个n以便您可以看到它的样子。 For example, you could do:例如,你可以这样做:

for n in nice_list:
    print(n)

to understand how that's working.了解它是如何工作的。

Slightly unconventional approach, due to unknown depth, and/or lack of known array flattening operation - I would try with regex:由于未知的深度和/或缺乏已知的数组展平操作,有点非常规的方法 - 我会尝试使用正则表达式:

import re

def getPos(el, arr):
    el=re.escape(str(el))
    el=f"(\({el})|({el}\))"
    i=0
    for s in re.finditer(r"\([^\)]+\)", str(arr)):
        if(re.match(el,s.group(0))):
            yield i
        i+=1

Which yields:其中产生:

>>> print(list(getPos(list2, list1)))

[0, 1, 4, 5, 6, 8, 9]

(Which I believe is the actual result you want). (我相信这是您想要的实际结果)。

Ok, so here you go好的,给你

This use recursion because we don't know the depth of your list1 SO the index will be counted like this :这使用递归,因为我们不知道您的list1的深度,因此索引将按如下方式计算:

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

etc... (The same order you have by writing it in 1 row)等等...(与您将其写在 1 行中的顺序相同)

Here the result will be :这里的结果将是:

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

Now the code现在代码

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 is the index we are working on count is the total amount of element (22). s是我们正在处理的索引count是元素 (22) 的总量。 Indexes is the list of index you want. Indexes是您想要的索引列表。

This work even if you make a list3 = [list1,list1,[list1,[list1],list1]] , you may want to try it.即使你制作了一个list3 = [list1,list1,[list1,[list1],list1]] ,你也可以尝试一下。

Best luck to end your script now.祝你好运现在​​结束你的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM