附加到列表错误:' str'对象没有属性'追加'

时间:2015-07-07 15:38:06

标签: python list

graph = []
for double in change:
    year = str(double[1].year)
    month = str(double[1].month)
    day = str(double[1].day)
    hour = str(double[1].hour)
    string = '{ x: new Date('+year+','+month+','+day+','+hour+'), '+'y:'+str(double[0])+'}, '
    graph.append(string)    
    graph = ''.join(graph)
    print "Graph is:"+graph  

我不明白为什么这不起作用并给出错误'str' object has no attribute 'append'

请注意change是列表清单。在这种特定情况下,它看起来像这样:

[[1.0, datetime.datetime(2015, 7, 3, 1, 0)], [2.0, datetime.datetime(2015, 7, 3, 2, 0)]]

目标是获得:

'{ x: new Date(2015,7,3,1), y:1.0}, ', '{ x: new Date(2015,7,3,2), y:2.0}, '

2 个答案:

答案 0 :(得分:1)

您正在尝试连接字符串和列表,这就是您收到该错误的原因。

import datetime
change = [[1.0, datetime.datetime(2015, 7, 3, 1, 0)], [2.0, datetime.datetime(2015, 7, 3, 2, 0)]]
graph = []
for double in change:
    year = str(double[1].year)
    month = str(double[1].month)
    day = str(double[1].day)
    hour = str(double[1].hour)
    string = '{ x: new Date('+year+','+month+','+day+','+hour+'), '+'y:'+str(double[0])+'}'
    graph.append(string)

print ', '.join(graph)

答案 1 :(得分:0)

graph = []
for double in change:
    year = str(double[1].year)
    month = str(double[1].month)
    day = str(double[1].day)
    hour = str(double[1].hour)
    string = '{ x: new Date('+year+','+month+','+day+','+hour+'), '+'y:'+str(double[0])+'}, '
    graph.append(string)

graph = ''.join(graph)
print "Graph is:"+graph