0

我正在尝试在 python 中操作项目列表,但我收到错误“AttributeError:'list' object has no attribute'split'”我知道列表不理解.split,但我不知道还能做什么. 下面是我的代码相关部分的复制粘贴。

tourl = 'http://data.bitcoinity.org/chart_data'
tovalues = {'timespan':'24h','resolution':'hour','currency':'USD','exchange':'all','mining_pool':'all','compare':'no','data_type':'price_volume','chart_type':'line_bar','smoothing':'linear','chart_types':'ccacdfcdaa'}
todata = urllib.urlencode(tovalues)
toreq = urllib2.Request(tourl, todata)
tores = urllib2.urlopen(toreq)
tores2 = tores.read()
tos = json.loads(tores2)
tola = tos["data"]
for item in tola:
    ting = item.get("values")
    ting.split(',')[2]       <-----ERROR
    print(ting)

要了解我想要做什么,您还需要查看 json 数据。Ting 输出:

[
  [1379955600000L, 123.107310846774], [1379959200000L, 124.092526428571],
  [1379962800000L, 125.539504822835], [1379966400000L, 126.27024617931],
  [1379970000000L, 126.723474983766], [1379973600000L, 126.242406356837],
  [1379977200000L, 124.788410570987], [1379980800000L, 126.810084904632],
  [1379984400000L, 128.270580796748], [1379988000000L, 127.892411269036],
  [1379991600000L, 126.140579640523], [1379995200000L, 126.513705084746],
  [1379998800000L, 128.695124951923], [1380002400000L, 128.709738051044],
  [1380006000000L, 125.987767097378], [1380009600000L, 124.323433535528],
  [1380013200000L, 123.359378559603], [1380016800000L, 125.963250678733],
  [1380020400000L, 125.074618194444], [1380024000000L, 124.656345088853],
  [1380027600000L, 122.411303435449], [1380031200000L, 124.145747100372],
  [1380034800000L, 124.359452274881], [1380038400000L, 122.815357211394],
  [1380042000000L, 123.057706915888]
]

[
  [1379955600000L, 536.4739135], [1379959200000L, 1235.42506637],
  [1379962800000L, 763.16329656], [1379966400000L, 804.04579319],
  [1379970000000L, 634.84689741], [1379973600000L, 753.52716718], 
  [1379977200000L, 506.90632968], [1379980800000L, 494.473732950001],
  [1379984400000L, 437.02095093], [1379988000000L, 176.25405034],
  [1379991600000L, 319.80432715], [1379995200000L, 206.87212398],
  [1379998800000L, 638.47226435], [1380002400000L, 438.18036666],
  [1380006000000L, 512.68490443], [1380009600000L, 904.603705539997],
  [1380013200000L, 491.408088450001], [1380016800000L, 670.275397960001],
  [1380020400000L, 767.166941339999], [1380024000000L, 899.976089609997], 
  [1380027600000L, 1243.64963909], [1380031200000L, 1508.82429811],
  [1380034800000L, 1190.18854705], [1380038400000L, 546.504592349999], 
  [1380042000000L, 206.84883264]
]

并且 ting[0] 输出:

[1379955600000L, 123.187067936508]
[1379955600000L, 536.794013499999]

我真正想做的是将第二个逗号之后的 ting[0-24] 的值相加。这使我尝试进行拆分,但这不起作用

4

2 回答 2

3

你已经有了一个清单;逗号由 Python 放在那里以仅在打印列表时分隔值。

只需直接访问元素 2 :

print ting[2]

这打印:

[1379962800000, 125.539504822835]

item['values'](so ) 中的每个条目ting都是两个浮点值的列表,因此您可以处理索引为 0 和 1 的每个条目:

>>> print ting[2][0]
1379962800000
>>> print ting[2][1]
125.539504822835

要获取所有第二个值的列表,您可以使用列表推导:

second_vals = [t[1] for t in ting]
于 2013-09-24T17:42:00.900 回答
1

当您使用 加载数据时json.loads,它已经被解析为一个真实的列表,您可以照常进行切片和索引。如果您想要从第三个元素开始的数据,只需使用ting[2:]. (如果您只想要第三个元素本身,只需使用ting[2].)

于 2013-09-24T17:41:34.603 回答