我有一个数据列表,就像这里缩短的数据一样。
[[743, 5043.0, 'N/A', 19.40393776, 7.18, 15.487], [744, 6117.0, 'N/A', 19.22138894, 49.29, 15.341], [745, 5152.0, 'N/A', 16.46983774, 7.94, 15.788], [746, 4856.0, 1.5936507936507938, 9.27358173, 2.47, 15.302], [747, 4537.0, 1.0317880794701988, 6.02930329, 2.85, 15.784], [748, 4989.0, 'N/A', 2.696370652, 1.58, 15.269], [749, 5185.0, 0.8675585284280938, 5.349553819, 2.55, 15.416], [749, 5185.0, 'N/A', 3.94105221, 1.7, 15.416], [749, 5185.0, 'N/A', 8.10904807, 1.3, 15.416]]
现在我有一个列表列表,第一个元素代表Kepler感兴趣对象编号。我对数据进行了排序,以便按照第一个元素的顺序显示列表。我的目标是创建一个列表列表,其中所有具有匹配的第一个元素的列表一起放在列表中。例如,有三个749
列表应该放在一个列表中。我正在努力创建一个迭代我所有列表的程序,并将第一个元素与未知数量的其他第一个元素进行比较。最简单的方法是什么?
目标:
[[[743, 5043.0, 'N/A', 19.40393776, 7.18, 15.487]], [[744, 6117.0, 'N/A', 19.22138894, 49.29, 15.341]], [[745, 5152.0, 'N/A', 16.46983774, 7.94, 15.788]], [[746, 4856.0, 1.5936507936507938, 9.27358173, 2.47, 15.302]], [[747, 4537.0, 1.0317880794701988, 6.02930329, 2.85, 15.784]], [[748, 4989.0, 'N/A', 2.696370652, 1.58, 15.269]], [[749, 5185.0, 0.8675585284280938, 5.349553819, 2.55, 15.416], [749, 5185.0, 'N/A', 3.94105221, 1.7, 15.416], [749, 5185.0, 'N/A', 8.10904807, 1.3, 15.416]]]
答案 0 :(得分:0)
我认为使用将列表列表转换为pandas DataFrame是一个很好的方法:
import pandas as pd
x = pd.DataFrame([[743, 5043.0, 'N/A', 19.40393776, 7.18, 15.487], [744, 6117.0, 'N/A', 19.22138894, 49.29, 15.341], [745, 5152.0, 'N/A', 16.46983774, 7.94, 15.788], [746, 4856.0, 1.5936507936507938, 9.27358173, 2.47, 15.302], [747, 4537.0, 1.0317880794701988, 6.02930329, 2.85, 15.784], [748, 4989.0, 'N/A', 2.696370652, 1.58, 15.269], [749, 5185.0, 0.8675585284280938, 5.349553819, 2.55, 15.416], [749, 5185.0, 'N/A', 3.94105221, 1.7, 15.416], [749, 5185.0, 'N/A', 8.10904807, 1.3, 15.416]])
y = x.groupby(0).apply(lambda z: list(z.values))
Goal = [[list(z) for z in y[idx]] for idx in y.index]
我相信这是按照你描述的方式进行格式化和分组的。
答案 1 :(得分:0)
如果你想在Python中使用内置功能,你可以使用字典。
from collections import OrderedDict
l = [[743, 5043.0, 'N/A', 19.40393776, 7.18, 15.487], [744, 6117.0, 'N/A', 19.22138894, 49.29, 15.341], [745, 5152.0, 'N/A', 16.46983774, 7.94, 15.788], [746, 4856.0, 1.5936507936507938, 9.27358173, 2.47, 15.302], [747, 4537.0, 1.0317880794701988, 6.02930329, 2.85, 15.784], [748, 4989.0, 'N/A', 2.696370652, 1.58, 15.269], [749, 5185.0, 0.8675585284280938, 5.349553819, 2.55, 15.416], [749, 5185.0, 'N/A', 3.94105221, 1.7, 15.416], [749, 5185.0, 'N/A', 8.10904807, 1.3, 15.416]]
d = OrderedDict()
for inner in l:
if inner[0] in d:
d[inner[0]].append(inner)
else:
d[inner[0]] = [inner]
list(d.values())
我使用了OrderedDict,因此它保留了原始项目的顺序。