提问人:jessicashew 提问时间:2/13/2022 更新时间:2/13/2022 访问量:44
Python - 为什么列表中的每个项目都被 while 循环的最终值覆盖?[复制]
Python - Why is every item in my list being overwritten by the final value of my while loop? [duplicate]
问:
我正在尝试从加速度计值计算位置。为此,我将每个加速度输出与时间戳一起附加到嵌套列表中,然后使用它们来计算速度和位置。我遇到了一个问题,即我在速度列表中附加的每个值都会被 while 循环中的最终速度输出覆盖。有没有办法保留循环每次迭代的速度值列表,而不会改变其值?
import time
from random import randint
acceleration_list = []
velocity_list = []
velocity = [[0,0,0],0]
index = 0
while index < 10:
# Get current acceleration and append to list
accel = [randint(-5,5),randint(-5,5),randint(-5,5)] # Accelerometer Values
accel[1] -= 1 # Subtract 1g from Y-value
time_stamp = time.time() # Time stamp
curr_acceleration = [accel, time_stamp] # List to store accelerometer values + time stamp
acceleration_list.append(curr_acceleration) # Add to total acceleration list
# print(f"Acceleration values are: {acceleration_list}")
time.sleep(0.025)
# Calculate velocity using acceleration
for i in range(3):
if len(acceleration_list) > 1 and index > 0: # Wait until we've done one iteration
if abs(acceleration_list[index][0][i]) < 0.5: # Filter out small values from the accelerometer
acceleration_list[index][0][i] = 0.0
# Previous Velocity + (Current Acceleration + Previous Acceleration / 2 ) * (Current Time - Previous Time)
velocity[0][i] = velocity_list[index-1][0][i] + ((acceleration_list[index][0][i] + acceleration_list[index-1][0][i])/2)*(acceleration_list[index][1] - acceleration_list[index-1][1])
velocity[1] = acceleration_list[index][1]
velocity_list.append(velocity)
print(velocity)
index += 1
print(velocity_list)
答: 暂无答案
评论
velocity
velocity
.append