我有一个numpy单元格坐标数组,其中第3个元素是坐标,第4个元素是时间步长,第5个元素是Cell ID。我想将它转换为列表列表,其中每个列表代表另一个单元格。 Adttionaly我想要一个numpy数组的numpy数组相同。因此,只要第5个元素(CellID)是另一个,就应该拆分列表/数组。所以第5个元素的数字从1000000000.0到1000000297.0,但之后它从1000000000.0开始到1000000166.0。问题是这些是不同的单元格,不应该合并在一起。我建议使用面具是最有效的方法,但我不知道如何。
示例:
输入:
[[ 2.43130000e+01 2.94679000e+02 1.50000000e+00 1.00000000e+00
1.00000000e+09]
[ 2.55100000e+01 2.95263000e+02 1.50000000e+00 2.00000000e+00
1.00000000e+09]
[ 2.67430000e+01 2.94526000e+02 1.50000000e+00 3.00000000e+00
1.00000000e+09]
...,
[ 2.82311000e+02 5.35420000e+01 1.50000000e+00 1.00000000e+02
1.00000017e+09]
[ 2.86946000e+02 5.49790000e+01 9.17700000e+00 1.01000000e+02
1.00000017e+09]
[ 2.93990000e+02 5.19340000e+01 1.29780000e+01 1.02000000e+02
1.00000017e+09]]
提取列表清单的输出
[[[130.463, 248.155, 55.5, 96.0, 1000000161.0], [132.628, 252.684, 52.356, 97.0, 1000000161.0], [137.9, 255.265, 46.883, 98.0, 1000000161.0], [132.053, 258.447, 45.077, 99.0, 1000000161.0], [134.003, 259.702, 44.496, 100.0, 1000000161.0], [135.481, 257.842, 39.964, 101.0, 1000000161.0], [140.443, 256.546, 33.973, 102.0, 1000000161.0], [219.541, 192.737, 1.5, 98.0, 1000000162.0], [[213.184, 188.749, 1.5, 99.0, 1000000162.0], [211.233, 183.231, 1.5, 100.0, 1000000162.0], [205.015, 180.092, 1.5, 101.0, 1000000162.0], [201.842, 177.759, 1.5, 102.0, 1000000162.0], [197.246, 175.923, 1.5, 103.0, 1000000162.0]], [[238.615, 95.605, 29.725, 98.0, 1000000163.0], [229.771, 90.084, 26.762, 99.0, 1000000163.0], [226.749, 82.741, 19.857, 100.0, 1000000163.0], [228.741, 78.017, 15.398, 101.0, 1000000163.0], [227.645, 75.927, 9.396, 102.0, 1000000163.0], [226.383, 66.733, 1.5, 103.0, 1000000163.0]], [[267.344, 47.023, 19.123, 98.0, 1000000164.0], [267.265, 43.635, 19.669, 99.0, 1000000164.0], [271.186, 47.123, 10.944, 100.0, 1000000164.0], [275.205, 45.156, 10.225, 101.0, 1000000164.0], [281.018, 48.663, 7.118, 102.0, 1000000164.0]], [[257.359, 225.509, 1.5, 103.0, 1000000165.0]], [[281.395, 58.163, 1.5, 98.0, 1000000166.0], [282.415, 55.211, 1.5, 99.0, 1000000166.0], [282.311, 53.542, 1.5, 100.0, 1000000166.0], [286.946, 54.979, 9.177, 101.0, 1000000166.0], [293.99, 51.934, 12.978, 102.0, 1000000166.0]]]
但是元组也没关系:
[[(130.463, 248.155, 55.5, 96.0, 1000000161.0), (132.628, 252.684, 52.356, 97.0, 1000000161.0), (137.9, 255.265, 46.883, 98.0, 1000000161.0), (132.053, 258.447, 45.077, 99.0, 1000000161.0), (134.003, 259.702, 44.496, 100.0, 1000000161.0), (135.481, 257.842, 39.964, 101.0, 1000000161.0), (140.443, 256.546, 33.973, 102.0, 1000000161.0), (219.541, 192.737, 1.5, 98.0, 1000000162.0), [(213.184, 188.749, 1.5, 99.0, 1000000162.0), (211.233, 183.231, 1.5, 100.0, 1000000162.0), (205.015, 180.092, 1.5, 101.0, 1000000162.0), (201.842, 177.759, 1.5, 102.0, 1000000162.0), (197.246, 175.923, 1.5, 103.0, 1000000162.0)], [(238.615, 95.605, 29.725, 98.0, 1000000163.0), (229.771, 90.084, 26.762, 99.0, 1000000163.0), (226.749, 82.741, 19.857, 100.0, 1000000163.0), (228.741, 78.017, 15.398, 101.0, 1000000163.0), ....]]
答案 0 :(得分:0)
如果具有一个单元格的时间步长的数组是连续的,我相信这应该有效:
list_of_lists = []
temporary_list = []
# store the current cellID:
cellID = list[0][-1]
for element in list:
if element[-1]==cellID:
temporary_list.append(element)
else:
list_of_lists.append(temporary_list)
temporary_list = []
cellID = element[-1]
# append the last temporary_list:
list_of_lists.append(temporary_list)