Home > Enterprise >  Filling up already made lists from another list
Filling up already made lists from another list

Time:06-15

I have a list with a couple values and i want to split that list into 3 more lists with a length of 8 into lists that are already named. I wrote the code below to do that and it almost works perfectly, the issue is just that in the output, every list starts with 0 and it skips over the value that is supposed to be in place of 0 (eg: 8 and 16). Why is this hapenning? This is just a test code i have for this example, my main code involves splitting a list with 3.4 million items into 13 lists of 262,144 but im getting the same issue where the first item in each of the 13 lists is replaced by the very first value in the main list.

Any help is very appreciated, Thanks!

list1 = []
list2 = []
list3 = []
LIST = [list1, list2, list3]
num = list(range(25))
print(num)
LastRow = len(num)
Cnt = 0
for listed in LIST:
    for num[Cnt] in num:
        while len(listed) < 8:
            listed.append(num[Cnt])
            if Cnt == LastRow-1:
                break
            Cnt = Cnt   1

print(list1)
print(list2)
print(list3)
print(LIST)

OUTPUT:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 9, 10, 11, 12, 13, 14, 15]
[0, 17, 18, 19, 20, 21, 22, 23]
[[0, 1, 2, 3, 4, 5, 6, 7], [0, 9, 10, 11, 12, 13, 14, 15], [0, 17, 18, 19, 20, 21, 22, 23]]

CodePudding user response:

It's because you modify your num list. If you print your num list at the end you will see that it has as many 0s as you are trying to split the list in to part.

Below is a working example of a function that splits the list in to a lists of wanted length and number of lists.

def separator(list, n, l):
    """"
    Separates the list in to n lists with the lenght l 
    The last list can be shorter than l
    """
    
    list_of_lists = []
    for i in range(n):
        list_of_lists.append(list[i*l:(i 1)*l])
        
    return list_of_lists

separator(list(range(25)), 3, 8)

# [[0, 1, 2, 3, 4, 5, 6, 7],
# [8, 9, 10, 11, 12, 13, 14, 15],
# [16, 17, 18, 19, 20, 21, 22, 23]]

If you do not need to have all of the lists in memory I advise to check generators that could generate each list as you need it.

def separator_gen(list, n, l):
    """"
    Separates the list in to n lists with the lenght l 
    The last list can be shorter than l
    """
    
    for i in range(n):
        yield list[i*l:(i 1)*l]

my_gen = separator_gen(list(range(25)), 3, 8)

next(my_gen)
# [0, 1, 2, 3, 4, 5, 6, 7]
next(my_gen)
# [8, 9, 10, 11, 12, 13, 14, 15]
next(my_gen)
#[16, 17, 18, 19, 20, 21, 22, 23]

next(my_gen)
# raises StopIteration error

CodePudding user response:

This is a rather convoluted way to do it. In python you can just iterate over a list.

Here is my simple approach:

starting_list = list(range(25))

tmp_list = []
split_lists = []

for elem in starting_list:
    if len(tmp_list) == 8:
        split_lists.append(tmp_list)
        tmp_list = []
    tmp_list.append(elem)
    if elem == starting_list[-1]:
        split_lists.append(tmp_list)


print(split_lists)
[[0, 1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23], [24]]

If you remove the last if statement then a tmp_list with less than 8 values in it will not be appended to split_lists. I'm not sure if this is intended or not.

  • Related