【发布时间】:2022-11-28 19:39:20
【问题描述】:
我有一个列表,其中包含许多子列表。每个子列表都有两个值。我想从每个子列表中的第二个值中减去第一个值,并将结果存储在新列表中。
现在这些新列表也是另一个列表列表的子列表。
例如,lists_of_lists1 是这样的:
lists_of_lists1 = [ran_list1, ran_list2, ran_list3, ran_list4, ran_list5, ran_list6,
ran_list7,ran_list8]
这是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]]
现在减去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]
所以ran_subresult1是一个空列表,ran_list1[1] - ran_list1[0]的结果将存储在其中,ran_subresult2将存储ran_list2[1] - ran_list2[0]的结果,依此类推...
我的尝试看起来像这样:
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 和 j 有点迷茫,我想我的方向是正确的,但我仍然做不到。我会很感激一些帮助。谢谢!
【问题讨论】: