1

So, I have a nested list (Let's call it A). It has a length of 2, and they should be considered separate lists. I want to iterate through these 2 separately, and remove the sublists that don't have equal length. For example, I want my output to be the same as A but with [['Table heading']] removed because it is not the same length as the other nested lists.

A=[[[['1'], ['2'], ['3'], ['4']],
  [['x'],['y'],['z'],['w']],
  [['a'],['b'],['c'],['d']],
  [['11'], ['22'], ['33'], ['44']]],
  [[['Table heading']],
  [['A'], ['B'], ['C'], ['D']],
  [['X'], ['Y'], ['Z'], ['W']],
  [['1'], ['2'], ['3'], ['4']]]]

 output=[[[['1'], ['2'], ['3'], ['4']],
  [['x'],['y'],['z'],['w']],
  [['a'],['b'],['c'],['d']],
  [['11'], ['22'], ['33'], ['44']]],
  [[['A'], ['B'], ['C'], ['D']],
  [['X'], ['Y'], ['Z'], ['W']],
  [['1'], ['2'], ['3'], ['4']]]]
14
  • What rule governs what gets removed? For instance, why isn't Table Heading kept as the length of the rest aren't equal to its length... and what'd happen if everything was a different length? Commented Nov 6, 2019 at 0:12
  • 1
    O_O can you simplify this example Commented Nov 6, 2019 at 0:12
  • the reason table heading gets removed is because it is not the same length as the other ones, all other ones have a length of 4, and table heading has a length of 1 Commented Nov 6, 2019 at 0:15
  • 2
    @Mel sure... but why isn't it the other way around? One could argue that Table Heading should stay as it's a length of 1 and all the others aren't length 1 so should be removed... so there must be some criteria? Is it for instance, they all are length 4 or they can't be length 1... and what'd happen in A[1] if the last row was [['1'], ['2'], ['3'], ['4'], ['5']] ? Commented Nov 6, 2019 at 0:23
  • 1
    @Mel what about ties? If there's 2 rows with 2 columns, and 2 rows with 4 columns... what should happen? Commented Nov 6, 2019 at 2:06

2 Answers 2

1

Like this

[ y for x in A for y in x if len(y) == 4]

Or in readable format.

out = []
for inner in A:
     for inner_inner in inner:
          if len(inner_inner) == 4:
               out.append(inner_inner)

If you don't know the size of inner list but, you know that there is only one list that do not match in size, you can do like this.

out = []
for inner in A:
     for inner_inner in inner:
         inner_size = len(inner_inner) 
         try:
             if inner_size == previous_size:
                 out.append(inner_inner)
          except NameError:
                  previous_size = inner_size
                  out.append(inner_inner)

This one have a default, if the first inner_inner element is to removed, the entire list elements will be removed instead of the first one.

Or like this

from collections import Counter

size = Counter([ len(y) for x in A for y in x ]).most_common(1)[0][0]
[ y for x in A for y in x if len(y) == size]

This solution loop two time over the entire list, depending on list sizes this could be a limitation.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this but this is hypothetical example, where I know the len of the inner, and I only want the sublists with len=4. But how would I set the len programiticlaly?
A=[[[['1'], ['2'], ['3'], ['4']], [['x'],['y'],['z'],['w']], [['a'],['b'],['c'],['d']], [['11'], ['22'], ['33'], ['44']]], [[['Table heading']], [['A'], ['B'], ['C']], [['X'], ['Y'], ['Z']], [['1'], ['2'], ['3']]]] How would I determine the length separately for each, for example, the most common length is 4 in the first table, and 3 in the second table. So I want the most common length in each table.
Based on your recommendations, this is how I would address if the lengths of the tables were different: A_new=[] for i in range(len(A)): Size=Counter([len(y) for y in A[i]]).most_common(1)[0][0] new=[num for num in A[i] if len(num)==Size] A_new.append(new)
@Mel update your post and add your comment in it, the format will be easier to read .
0

Check this out. It goes to delete the element unless they are not list anymore.


def  delete_odd(A):
    if isinstance(A[0],list)==False:
        return
    def delete_elem(a,if_not):
        print(a,'  ',if_not,'   ')

        i = 0
        while i<len(a):
            if len(a[i])!=if_not:
                del a[i]
            else:
                i+=1

    if len(A)<=2:
        pass
    else:
        n1 = len(A[0])
        n2 = len(A[1])
        n3 = len(A[2])
        if n1==n2:
            n = n1
        elif n2==n3:
            n = n2

        delete_elem(A,if_not=n)
    i = 0
    while i<len(A):
        delete_odd(A[i])
        i+=1




A = [[[['1'], ['2'], ['3'], ['4']],
  [['x'],['y'],['z'],['w']],
  [['a'],['b'],['c'],['d']],
  [['11'], ['22'], ['33'], ['44']]],
  [[['Table heading']],
  [['A'], ['B'], ['C'], ['D']],
  [['X'], ['Y'], ['Z'], ['W']],
  [['1'], ['2'], ['3'], ['4']]]]
delete_odd(A)

print(A)
#output
[[[['1'], ['2'], ['3'], ['4']], [['x'], ['y'], ['z'], ['w']], [['a'], ['b'], ['c'], ['d']], [['11'], ['22'], ['33'], ['44']]], [[['A'], ['B'], ['C'], ['D']], [['X'], ['Y'], ['Z'], ['W']], [['1'], ['2'], ['3'], ['4']]]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.