取决于您希望如何构建数据。
这是一个替代方案:
>>> data_point = {}
>>> data_point[10] = (1, 34, 23 )
>>> data_point[11] = (1, 34, 54 )
>>> data_point[12] = (1, 42, 4 )
>>> data_point[13] = (1, 76, 3 )
>>> data_point[14] = (1, 35, 56 )
>>> data_point[15] = (1, 23, 4 )
>>> data_point[16] = (2, 56, 5 )
>>> data_point[17] = (2, 46, 6 )
>>> data_point[18] = (2, 35, 77 )
>>> data_point[19] = (3, 75, 6 )
>>> data_point[20] = (3, 54, 7 )
>>> data_point[21] = (3, 43, 6 )
类型的所有X
值1
:
>>> [x[1] for x in data_point.itervalues() if x[0] == 1]
[34, 34, 42, 76, 35, 23]
类型的所有Y
值1
:
>>> [x[2] for x in data_point.itervalues() if x[0] == 1]
[23, 54, 4, 3, 56, 4]
类型的所有X
值2
:
>>> [x[1] for x in data_point.itervalues() if x[0] == 2]
[56, 46, 35]
类型的所有Y
值2
:
>>> [x[2] for x in data_point.itervalues() if x[0] == 2]
[5, 6, 77]
.. 等等。
您将通过以下方式获得的总和sum()
:
>>> sum([x[1] for x in data_point.itervalues() if x[0] == 1])
244
>>> sum([x[2] for x in data_point.itervalues() if x[0] == 1])
144
>>> sum([x[1] for x in data_point.itervalues() if x[0] == 2])
137
>>> sum([x[2] for x in data_point.itervalues() if x[0] == 2])
88