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))