简体   繁体   English

列表的列表,减去每个子列表中的值并将结果存储在新的子列表中

[英]A list of lists, substract the values in each sub-list and store the results in new sub-lists

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是这样的:

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.这是ran_list1 ,一个子列表。 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:现在减去ran_list1[1] - ran_list1[0] (以这种方式对每个子列表),结果存储在这里:

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...所以ran_subresult1是一个空列表, ran_list1[1] - ran_list1[0]的结果将存储在其中,而ran_subresult2将存储ran_list2[1] - ran_list2[0]的结果,依此类推......

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.我对ij有点迷茫,我想我的方向是正确的,但我仍然做不到。 I'll appreciate some help with this.我会很感激一些帮助。 Thanks!谢谢!

EDIT - This is the expected output. From lists_of_lists1 , let's take the first sub-list as an example, which is ran_list1 .编辑 - 这是预期的 output。从lists_of_lists1 ,我们以第一个子列表为例,即ran_list1 The values inside ran_list1 are pairs of numbers: ran_list1中的值是数字对:

[[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 in this manner:现在以这种方式减去:

47.84539466004288 - 34.39460533995712 = 13.451
46.50422752199436 - 33.095772478005635 = 13.409

And so on...等等...

Now those results will be stored inside ran_subresult1 , which is the first sub-list inside list_of_lists2 .现在这些结果将存储在ran_subresult1中,这是list_of_lists2中的第一个子列表。

Hence, ran_subresult1 would be [13.451, 13.409.....]因此, ran_subresult1将是[13.451, 13.409.....]

And so on for each sub-list.每个子列表依此类推。

This is an alternative approach for your MWE:这是您的 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]]

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).为此使用numpy ,它使处理多维 arrays 变得更加容易并且效率更高(因为它是用 C 编写的)。 To start off, you have to install numpy .首先,您必须安装 numpy Then, in your code, import it.然后,在您的代码中导入它。 The convention is to import it as np:约定是将其作为 np 导入:

import numpy as np

Next we want to turn your list of lists into a numpy array.接下来我们要将您的列表列表转换为 numpy 数组。 If your different ran_list s have different dimensions, you will have to treat them independently, which is what I'll do here.如果您的不同ran_list具有不同的维度,您将必须独立对待它们,这就是我在这里要做的。 (If they all have the same shape, you can treat it as one 3-d array.): (如果它们都具有相同的形状,您可以将其视为一个 3 维数组。):

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.在这里,我将 numpy arrays 转回嵌套列表,但如果您想继续对它们进行计算,最好将它们保留为 numpy arrays。

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.我们首先得到列表列表的每个元素(一个长度为2的列表),然后append后者列表(i)个元素之间的差异。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM