假设我有一个列表,例如:
my_list=['0','2','3','5','1']
我希望通过每个数字按照一个序列相互减去来创建一个新的列表,以如下方式呈现(每个数字向左减去):
0-nothing=0
2-0=2
3-2=1
5-3=2
1-5=-4
new_list=['0','2','1','2','-4']
我觉得这个问题可以在一个for循环中解决。以下是我的可怕的for循环代码,以及手动完成的过程:
new_list = []
for item in my_list:
new_list.append(float(item)) #Converting strings into floats
print(new_list)
#for number in my_list:
#print(list(str((number-[0:])))) #completely wrong
new_list[0]=new_list[0]
new_list[1]=new_list[1]-new_list[0]
new_list[2]=new_list[2]-new_list[1] #Won't work past this point as the numbers in the list are now new.
new_list[3]=new_list[3]-new_list[2]
new_list[4]=new_list[4]-new_list[3]
print(new_list)
我尝试的每件事都是灾难。我的低劣逻辑是否导致了我走错了路?