gpt4 book ai didi

python - 对于列表中的索引

转载 作者:行者123 更新时间:2023-11-28 22:40:26 25 4
gpt4 key购买 nike

我正在努力寻找如何将减法的答案简化为小数点后两位。将 Enter 和 Leave 这两个列表相减,这样我就可以得到汽车从 A 点到 B 点所需的时间。

Enter=[7.12,
7.15,
7.24,
7.45,
7.28,
7.31,
7.18,
7.25,
7.33,
7.38]

Leave=[7.56,
7.24,
7.48,
7.52,
7.45,
7.57,
7.22,
7.31,
7.37,
7.41]

Timestaken=[]

for enter_data, leave_data in zip(Enter, Leave):
Timestaken.append(leave_data-enter_data)

print (Timestaken)

for index in Timestaken:
Timestaken[index]="%.2f" % Timestaken[index]

print(Timestaken)

当我在 IDLE 中运行它时,我收到以下信息:

>>> 
[0.4399999999999995, 0.08999999999999986, 0.2400000000000002, 0.0699999999999994, 0.16999999999999993, 0.2600000000000007, 0.040000000000000036, 0.05999999999999961, 0.040000000000000036, 0.03000000000000025]
Traceback (most recent call last):
File "M:\Task 2 Testing.py", line 46, in <module>
Timestaken[index]="%.2f" % Timestaken[index]
TypeError: list indices must be integers, not float

我需要这方面的帮助:

for index in Timestaken:
Timestaken[index]="%.2f" % Timestaken[index]

最佳答案

这应该有效:

for value in Timestaken:
print("%.2f" % value)

关于python - 对于列表中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33676050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com