Home > OS >  A list of lists, substract the values in each sub-list and store the results in new sub-lists
A list of lists, substract the values in each sub-list and store the results in new sub-lists

Time:11-28

I have a list, that contains many sub-lists. Each sub-list, has two values. I want to substract the first value from the second value in each sub-list, and store the results in new lists.

Now those new lists are also sub-lists, of another list of lists.

So for example, lists_of_lists1 is something like this:

lists_of_lists1 = [ran_list1, ran_list2, ran_list3, ran_list4, ran_list5, ran_list6,
          ran_list7,ran_list8]

And this is ran_list1, a sub-list. All sub-lists look similar to this.

[[34.39460533995712, 47.84539466004288],
 [33.095772478005635, 46.50422752199436],
 [36.66750709361337, 44.44360401749775],
 [33.33459042563053, 42.14689105585095],
 [36.638367322851444, 43.62250224236595],
 [36.465767572400296, 49.200899094266376],
 [32.220702473831686, 42.65929752616831],
 [34.31937169660605, 41.14216676493242],
 [31.198269305510344, 42.801730694489656],
 [31.216878962221035, 40.6092079943007],
 [28.465488368524227, 38.793770890735026],
 [34.50342917911651, 45.32990415421682]]

Now substract ran_list1[1] - ran_list1[0] (for each sublist in this manner), and the results store in here:

list_of_lists2 = [ran_subresult1 , ran_subresult2 , ran_subresult3 , ran_subresult4 ,
            ran_subresult5 , ran_subresult6 , ran_subresult7, ran_subresult8]

So ran_subresult1, is an empty list that the results of ran_list1[1] - ran_list1[0] would be store in it, and ran_subresult2 would store the resuls of ran_list2[1] - ran_list2[0], and so on...

My try of this look like this:

for i in lists_of_lists1:
    for j in range(len(i)):
        list_of_lists2[j].append(lists_of_lists1[j][1] - lists_of_lists1[j][0])

I got a bit lost with the i and j, I guess I'm in the right direction but I'm still unable to do it. I'll appreciate some help with this. Thanks!

CodePudding user response:

This is an alternative approach for your MWE:

main_list = [
    [
        [34.39460533995712, 47.84539466004288],
        [33.095772478005635, 46.50422752199436],
        [36.66750709361337, 44.44360401749775],
        [33.33459042563053, 42.14689105585095],
        [36.638367322851444, 43.62250224236595],
        [36.465767572400296, 49.200899094266376],
        [32.220702473831686, 42.65929752616831],
        [34.31937169660605, 41.14216676493242],
        [31.198269305510344, 42.801730694489656],
        [31.216878962221035, 40.6092079943007],
        [28.465488368524227, 38.793770890735026],
        [34.50342917911651, 45.32990415421682],
    ]
]

res_list = main_list.copy()
for i, main_i in enumerate(main_list):
    for j, main_i_j in enumerate(main_i):
        res_list[i][j] = [main_i_j[1] - main_i_j[0]]

CodePudding user response:

Use numpy for this, it makes dealing with multidimensional arrays so much easier and it is far more efficient (because it is written in C). To start off, you have to install numpy. Then, in your code, import it. The convention is to import it as np:

import numpy as np

Next we want to turn your list of lists into a numpy array. If your different ran_lists have different dimensions, you will have to treat them independently, which is what I'll do here. (If they all have the same shape, you can treat it as one 3-d array.) :

list_of_lists2 = []
for sub_list in lists_of_lists1:
    new_sub_list = np.array(sub_list)
    new_sub_list = new_sub_list[:,1] - new_sub_list[:,0]
    list_of_lists2.append(new_sub_list.tolist())

Here I have turned the numpy arrays back into nested lists but if you want to continue to do calculations with them it's probably better to keep them as numpy arrays.

CodePudding user response:

This should work. We first get each element of the list of lists (a list with length 2) and then append the difference between the latter lists (i) elements.

list = [[1, 23], [3, 2], [32, 213], [2321, 23]]
res_list = []
for i in list:
    res_list.append((i[1]-i[0]))

print(res_list)
  • Related