冷凝串行循环

时间:2012-08-14 00:47:17

标签: python for-loop

一直在敲我的脑袋几个小时,这一切都很干净,但我觉得它可以得到清洁。

center1_x = np.mean([q[0] for q in coordinates if q[-1]==0])
center2_x = np.mean([q[0] for q in coordinates if q[-1]==1])
center3_x = np.mean([q[0] for q in coordinates if q[-1]==2])
centers_x=[center1_x,center2_x,center3_x]

坐标是一个子列表列表,其中每个子列表包含[0]的X坐标,[1]的y坐标和[-1]的bin编号,即:

[[338.766845703125, 285.3931884765625, 0], [198.39230346679688, 184.1259307861328, 1], [356.7226867675781, 281.6376953125, 0], [200.93394470214844, 194.82655334472656, 1], [228.03591918945312, 297.79864501953125, 2], [226.06724548339844, 295.5475769042969, 2], [246.51475524902344, 280.373779296875, 2], [201.25830078125, 184.09994506835938, 1]]

1 个答案:

答案 0 :(得分:3)

尝试

centers_x = [np.mean([q[0] for q in coordinates if q[-1]==n]) for n in range(3)]