Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在将数字添加到列表时会将其拆分 def平均增长率(存量、时间): a=0 b=时间-1 平均时间=[] 而b_Python_Dictionary - Fatal编程技术网

Python 为什么在将数字添加到列表时会将其拆分 def平均增长率(存量、时间): a=0 b=时间-1 平均时间=[] 而b

Python 为什么在将数字添加到列表时会将其拆分 def平均增长率(存量、时间): a=0 b=时间-1 平均时间=[] 而b,python,dictionary,Python,Dictionary,+=用于连接列表,而不是附加元素。或使用: def average_growth(stock,time): a=0 b=time-1 average_time=[] while b<13: x=int(((stock[b]-stock[a])/time)) average_time+=str(x) a+=1 b+=1 print(x) average_growth({0: 317.68

+=
用于连接列表,而不是附加元素。或使用:

def average_growth(stock,time):
    a=0
    b=time-1
    average_time=[]
    while b<13:
        x=int(((stock[b]-stock[a])/time))
        average_time+=str(x)
        a+=1
        b+=1
    print(x)

average_growth({0: 317.68, 1: 396.05, 2: 451.48, 3: 428.03, 4: 516.26, 5: 604.83},2)


+=
用于连接列表,而不是附加元素。或使用:

def average_growth(stock,time):
    a=0
    b=time-1
    average_time=[]
    while b<13:
        x=int(((stock[b]-stock[a])/time))
        average_time+=str(x)
        a+=1
        b+=1
    print(x)

average_growth({0: 317.68, 1: 396.05, 2: 451.48, 3: 428.03, 4: 516.26, 5: 604.83},2)


你应该把str(x)放在它自己的列表中。也就是说,试试[str(x)]。您还可以在平均_时间中存储数字,而不是字符串。即平均时间+=[x]。然后,您可以使用map(str,x)将列表中的元素转换为字符串。请在代码中将str(x)放入自己的列表中。也就是说,试试[str(x)]。您还可以在平均_时间中存储数字,而不是字符串。即平均时间+=[x]。然后可以使用map(str,x)将列表中的元素转换为字符串
average_time.append(str(x))