Home > OS >  Iterate over list of lists and deduce a final list so that group by can be done on each element of f
Iterate over list of lists and deduce a final list so that group by can be done on each element of f

Time:11-05

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

[('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. 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')])
  • 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')]).
  • 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')])

CodePudding user response:

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.

CodePudding user response:

If the structure is just a described, i.e. 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:

[('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')]
  • Related