Home > other >  Splitting a list of lists in specific intervals in Python
Splitting a list of lists in specific intervals in Python

Time:03-19

I have a really big list of lists with integers that is also sorted from low to high. Also every nested list is an arithmetic sequence increasing by 1 . To make it more clear it could look something like:

f = [[0,1], [3], [7,8,9,10,11,12], [15,16], [18], [22,23,24], [39,40,41], [49,50,51]]

My goal is to split the nested big list into smaller nested lists. My first list must have numbers between 0 and 10, my second list must have numbers between 20 and 30 , my third between 40 to 50 etc. So I was wondering if there is a way to code in python to get the following lists:

f1 = [[0,1],[3],[7,8,9,10]]
f2 = [[22,23,24]]
f3 = [[40,41],[49,50]]

CodePudding user response:

Here is one way to do so:

data = []
for i in range(0, f[-1][-1], 20):
    new_seqs = []
    for seq in f:
        if i - len(seq)   1 <= seq[0] <= i   10:
            new_nums = []
            for num in seq:
                if i <= num <= i   10:
                    new_nums.append(num)
            new_seqs.append(new_nums)
    data.append(new_seqs)
print(data)

The same using list comprehension:

data = [[[num for num in seq if i <= num <= i   10] for seq in f if i - len(seq)   1 <= seq[0] <= i   10] for i in range(0, f[-1][-1], 20)]
print(data)

Output:

[[[0, 1], [3], [7, 8, 9, 10]], [[22, 23, 24]], [[40, 41], [49, 50]]]

  • We run a for loop from 0 to the largest element in the list (f[-1][-1]), increasing by 20 each time.
  • For each sub-list we check if at least one element is included between i and i 10. As it is an arithmetic sequence of common ratio 1, we only have to compare the first term.
  • If there is at least one term in the interval, we compare each number with i and i 10.

CodePudding user response:

I guess, similar to this is what you are looking for?

from collections import defaultdict
mem = defaultdict(list)
# expectations: each sub-list  is stickly between x * 10 and (x 1) * 10 where x is a number

def find_list_number(num, i=10):
  if num < i:
    return int(i / 10)
  else:
    return find_list_number(num, i   10)


for sub_list in flist:
  my_max = max(sub_list)
  key = find_list_number(my_max)
  mem[key].append(sub_list)

for k, v in mem.items():
  print((k, v))

Sample Output for above

(1, [[0, 1], [3]])
(2, [[7, 8, 9, 10, 11, 12], [15, 16], [18]])
(3, [[22, 23, 24]])
(5, [[39, 40, 41]])
(6, [[49, 50, 51]])

Note: [7,8,9,10,11,12] is in different class - not bug here. But you can modify conditions as you need & add additional conditions as it suits you. This is a sample, only to guide you.

  • Related