简体   繁体   English

迭代列表列表并推导出最终列表,以便可以对 Python 中最终列表的每个元素进行分组

[英]Iterate over list of lists and deduce a final list so that group by can be done on each element of final list in python

I am having a list of lists like below我有一个列表,如下所示

lst1 = [['sg'], ['sci'], ['op1', 'op2', 'op3'], ['pop1', 'pop2', 'pop3'], ['pn'], [['on'], ['pcat1', 'pcat2', 'pcat3'], ['oci']]]

I want to iterate over this list of lists (lst1) and get the result like below list of tuples or list of lists我想遍历这个列表列表(lst1)并得到如下元组列表或列表列表的结果

[('sg',), ('sg', 'sci'), ('sg', 'op1'), ('sg', 'op1', 'op2'), ('sg', 'op1', 'op2', 'op3'), ('sg', 'pop1'), ('sg', 'pop1', 'pop2'), ('sg', 'pop1', 'pop2', 'pop3'), ('sg', 'pn'), ('sg', 'on', 'pcat1', 'pcat2', 'pcat3', 'oci'), ('sg', 'on'), ('sg', 'pcat1'), ('sg', 'pcat1', 'pcat2'), ('sg', 'pcat1', 'pcat2', 'pcat3'), ('sg', 'oci')]

Explanation:解释:

  • first element of the lst1 is master element. lst1 的第一个元素是主元素。 It will be there with each tuple.每个元组都会在那里。
  • If the element is list with one element (like ['sci']) then it is added with master element and formed group (like [('sg', 'sci')])如果元素是具有一个元素的列表(如 ['sci']),则将其添加主元素并形成组(如 [('sg', 'sci')])
  • If the element of the lst1 is list with more than one value but not nested list (like ['op1', 'op2', 'op3']) then each element will be forming a group (like [('sg', 'op1'), ('sg', 'op1', 'op2'), ('sg', 'op1', 'op2', 'op3')]).如果 lst1 的元素是具有多个值但不是嵌套列表的列表(如 ['op1', 'op2', 'op3']),则每个元素将形成一个组(如 [('sg', ' op1'), ('sg', 'op1', 'op2'), ('sg', 'op1', 'op2', 'op3')])。
  • If the element of the lst1 is list of lists (like [['on'], ['pcat1', 'pcat2', 'pcat3'], ['oci']]) then that is flattened and formed one group (like [('sg', 'on', 'pcat1', 'pcat2', 'pcat3', 'oci')]) and all other individual groups as above (like [('sg', 'on'), ('sg', 'pcat1'), ('sg', 'pcat1', 'pcat2'), ('sg', 'pcat1', 'pcat2', 'pcat3'), ('sg', 'oci')])如果 lst1 的元素是列表列表(如 [['on'], ['pcat1', 'pcat2', 'pcat3'], ['oci']]),那么它被展平并形成一个组(如[('sg', 'on', 'pcat1', 'pcat2', 'pcat3', 'oci')]) 和上述所有其他单个组(如 [('sg', 'on'), (' sg', 'pcat1'), ('sg', 'pcat1', 'pcat2'), ('sg', 'pcat1', 'pcat2', 'pcat3'), ('sg', 'oci')] )
lst1 = [['sg'], ['sci'], ['op1', 'op2', 'op3'], ['pop1', 'pop2', 'pop3'], ['pn'], [['on'], ['pcat1', 'pcat2', 'pcat3'], ['oci']]]

master=lst1[0]
non_masters=lst1[1:]


def appendMasterSimple(item):
    lists_to_return=[]
    for i in range(0,len(item)):
        lists_to_return.append(master+item[0:i+1])
    return lists_to_return

def appendMasterEmbedded(item):
    lists_to_return=[]
    flat_list=[]
    for sublist in item:
        flat_list.extend(sublist)
    lists_to_return.append(master+flat_list)
    for sublist in item:
        lists_to_return.extend(appendMasterSimple(sublist))
    return lists_to_return

output=[]
for item in non_masters:
    if type(item[0])==list:
        output.extend(appendMasterEmbedded(item))
    else:
        output.extend(appendMasterSimple(item))
        
        
        

Edit: Actually your specs call for output=master+output at the very end if you want ['sg'] to be in the front.编辑:实际上,如果您希望 ['sg'] 位于最前面,那么您的规范最后会要求 output=master+output。

If the structure is just a described, ie there are no deeper levels etc., then you could try this:如果结构只是一个描述,即没有更深的层次等,那么你可以试试这个:

tree = [tuple(lst1[0])]
for sublist in lst1[1:]:
    if all(isinstance(item, str) for item in sublist):
        last = tree[0]
        for item in sublist:
            tree.append(last + (item,))
            last = tree[-1]
    else:
        base = tree[0]
        extension = []
        for subsublist in sublist:
            base = base + tuple(subsublist)
            last = tree[0]
            for item in subsublist:
                extension.append(last + (item,))
                last = extension[-1]
        tree.append(base)
        tree.extend(extension)

Result - tree - for your sample:结果 - tree - 您的样本:

[('sg',),
 ('sg', 'sci'),
 ('sg', 'op1'),
 ('sg', 'op1', 'op2'),
 ('sg', 'op1', 'op2', 'op3'),
 ('sg', 'pop1'),
 ('sg', 'pop1', 'pop2'),
 ('sg', 'pop1', 'pop2', 'pop3'),
 ('sg', 'pn'),
 ('sg', 'on', 'pcat1', 'pcat2', 'pcat3', 'oci'),
 ('sg', 'on'),
 ('sg', 'pcat1'),
 ('sg', 'pcat1', 'pcat2'),
 ('sg', 'pcat1', 'pcat2', 'pcat3'),
 ('sg', 'oci')]

暂无
暂无

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

相关问题 迭代 Python 列表列表并删除每个子列表的最终索引,无导入 - Iterate Python List of Lists and Remove Final Index of Each Sublist, No Imports 需要遍历两个列表并吐出最终列表,但能够从其中一个列表的任何元素开始 - Need to iterate through two lists and spit out a final list but being able to start on any element of one of the lists 用python中的另一个列表遍历列表列表 - iterate over a list of lists with another list in python 过滤列表列表python,如何创建最终列表? - filtering lists of lists python, how to create final list? 如何添加带有列表列表的列表,以便每个索引都是包含 python 中的元素和列表的列表 - How to add a list with a list of lists so that each index would be a list containing a an element and a list in python 如何迭代重复Python中每个元素的列表 - How to iterate over a list repeating each element in Python 如果列表在 Python 中,如何迭代列表? - How to iterate over a list if lists in Python? Python:合并两个列表,覆盖第一个列表的最后几行 - Python: Combine two lists, overwriting the final few lines of the first list 如何遍历列表列表? - How can I iterate over a list of lists? 如何在将列表中的每个值传递到 API 并在每个列表列表后暂停时迭代列表列表? - How to iterate over list of lists while passing each value in the lists into API and pausing after each list of list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM