0

以下是我正在尝试处理的数据。这是一个列表,我想进一步分解,以便于参考和处理。

我想添加一个 X 和 Y 标头,以便我可以通过调用 list['X']['Y'] 轻松调用它的值。这可以在 Python 中完成吗?

['0.0740,0.0730,0.0720,0.0300,0.0290,0.0240,0.0210,0.0200', '0.0200,0.0200,0.0150', '0.0260,0.0260,0.0200,0.0200,0.0200,0.0150', '0.0210,0.0210,0.0200,0.0170', '0.0770,0.0760,0.0740,0.0550,0.0450,0.0210', '0.0530,0.0520,0.0510,0.0230,0.0220', '0.0160,0.0160']

如果有人能告诉我怎么做,将不胜感激。

4

1 回答 1

1

您的列表当前包含一个维度,以逗号分隔的字符串表示您的价格。

只需使用.split(',')将这些字符串转换为列表;列表理解使这变得轻而易举:

splitlist = [prices.split(',') for prices in yourlist]

这会产生一个列表列表:

[['0.0740', '0.0730', '0.0720', '0.0300', '0.0290', '0.0240', '0.0210', '0.0200'], ['0.0200', '0.0200', '0.0150'], ['0.0260', '0.0260', '0.0200', '0.0200', '0.0200', '0.0150'], ['0.0210', '0.0210', '0.0200', '0.0170'], ['0.0770', '0.0760', '0.0740', '0.0550', '0.0450', '0.0210'], ['0.0530', '0.0520', '0.0510', '0.0230', '0.0220'], ['0.0160', '0.0160']]

并且splitlist[0][0]是字符串'0.0740'

如果您需要以浮点值而不是字符串结束,只需map()在拆分字符串上应用:

splitlist = [map(float, prices.split(',')) for prices in yourlist]

以便将每个单独的价格映射到float()调用。现在你有:

[[0.073999999999999996, 0.072999999999999995, 0.071999999999999995, 0.029999999999999999, 0.029000000000000001, 0.024, 0.021000000000000001, 0.02], [0.02, 0.02, 0.014999999999999999], [0.025999999999999999, 0.025999999999999999, 0.02, 0.02, 0.02, 0.014999999999999999], [0.021000000000000001, 0.021000000000000001, 0.02, 0.017000000000000001], [0.076999999999999999, 0.075999999999999998, 0.073999999999999996, 0.055, 0.044999999999999998, 0.021000000000000001], [0.052999999999999999, 0.051999999999999998, 0.050999999999999997, 0.023, 0.021999999999999999], [0.016, 0.016]]

splitlist[0][0]具有浮点值0.073999999999999996

于 2012-12-15T11:52:05.367 回答