For index in list

时间:2015-11-12 16:12:11

标签: python for-loop indexof

I'm struggling with finding out how to simplify answers from a subtraction to 2 decimal places. The two lists Enter and Leave are subtracted so I can get the time it takes for a car to get from point A to 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)

When I run it in IDLE, I receive the following:

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

I need help with this bit:

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

4 个答案:

答案 0 :(得分:1)

这应该有效:

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

答案 1 :(得分:1)

您对index

的假设错误
for index in Timestaken:

此代码将遍历Timestaken列表的所有,而不是其索引:

In [9]: [index for index in Timestaken]
Out[9]:
[0.4399999999999995,
 0.08999999999999986,
 0.2400000000000002,
 0.0699999999999994,
 0.16999999999999993,
 0.2600000000000007,
 0.040000000000000036,
 0.05999999999999961,
 0.040000000000000036,
 0.03000000000000025]

如果您真的想要索引和值,请使用enumerate

In [10]: [(index,value) for index,value in enumerate(Timestaken)]
Out[10]:
[(0, 0.4399999999999995),
 (1, 0.08999999999999986),
 (2, 0.2400000000000002),
 (3, 0.0699999999999994),
 (4, 0.16999999999999993),
 (5, 0.2600000000000007),
 (6, 0.040000000000000036),
 (7, 0.05999999999999961),
 (8, 0.040000000000000036),
 (9, 0.03000000000000025)]

使用您的代码:不需要那些appends并重新格式化

times_taken = [leave_data-enter_data for enter_data, leave_data in zip(Enter, Leave)]


print (times_taken)

times_taken_float = ["{%.2f}".format(value) for value in times_taken]

print (times_taken_float)

我使用列表理解来简化操作

答案 2 :(得分:1)

当你这样做时

for index in xrange(len(Timestaken)):
    Timestaken[index]="%.2f" % Timestaken[index]

您正在使用列表中的值Timestaken而不是那些值的索引

您需要以下内容

range

此方法适用于python 2.x,如果你使用python 3.x使用xrange而不是xrange(len(Timestaken))

其中first_halve :: [a] -> [a] first_halve = (\xs -> case xs of [] -> [] xs -> take ((length xs) `div` 2 ) xs) second_halve :: [a] -> [a] second_halve = (\xs -> case xs of [] -> [] xs -> drop ((length xs) `div` 2 ) xs) halve :: [a] -> ([a],[a]) halve = (\xs -> case xs of [] -> ([],[]) xs -> (first_halve xs, second_halve xs)) 返回一个生成0,1,...... len(Timestaken)-1的生成器

答案 3 :(得分:1)

更多pythonic:

>>> 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=["%.2f" % (leave_data-enter_data) for enter_data, leave_data in zip(enter, leave)]
>>> print (timestaken)
['0.44', '0.09', '0.24', '0.07', '0.17', '0.26', '0.04', '0.06', '0.04', '0.03']
>>>