'values keep piling append function python
i have a list li_LBF
which has 287 list inside it..i want calculate the Euclidean Distance between every list..but my code not working...the list where i am trying to store the output li_LBF_1
keeps getting bigger every time i run the code ..first time it was 287..2nd time i ran it it became 2x287..and keeps getting bigger..
li1=[]
li_LBF=[[1257.0, 1004.0, 284.0, 135.0, 100.0, 54.0, 47.0, 38.0, 56.0, 75.0, 104.0, 147.0, 154.0, 316.0, 101.0, 203.0, 87.0, 153.0, 88.0, 97.0, 97.0, 153.0, 339.0, 825.0, 1430.0, 11856.0], [1249.0, 998.0, 291.0, 140.0, 103.0, 60.0, 43.0, 39.0, 61.0, 82.0, 112.0, 154.0, 160.0, 312.0, 108.0, 209.0, 90.0, 149.0, 89.0, 100.0, 94.0, 162.0, 327.0, 831.0, 1410.0, 11827.0],........]]
def LBF_conversion(li_LBF):
for i in range(len(li_LBF)):
if i==0:
list2=li_LBF[i]
i+=1
else:
list1=list2
list2=li_LBF[i]
dist = euclidean(list1, list2)
li_LBF_1.append(dist)
return li_LBF_1
li1=LBF_conversion(li_LBF)
print(li1)
Solution 1:[1]
The problem is that you're not initializing li_LBF_1
. So you're referencing the global variable, which retains the value from the previous call, and you add more elements to it.
Put li_LBF_1 = []
at the beginning of the function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Barmar |