在我的代码中转换速度

时间:2015-12-18 10:04:03

标签: python python-3.x

我之前已经编写了一个代码,但只是让我的老师知道速度应该是mph,而不是mps。我做了必要的更改,但我一直收到错误。代码的上下文并不重要。

import re

# DATA
distance = 0.06 # Distance between the Camera A and B; 0.06 = 600 metres
speed_limit = 20 # (meters per second)

number_plates = ["DV61 GGB",      #UK
                 "D31 EG 2A",     #F
                 "5314 10A02",    #F
                 "24TEG 5063",    #F
                 "TR09 TRE",      #UK
                 "524 WAL 75",    #F
                 "TR44 VCZ",      #UK
                 "FR52 SWD",      #UK
                 "100 GBS 12",    #F
                 "HG55 BPO"       #UK
                 ]

enter = [7.12,7.17,7.22,7.12,7.23,7.41,7.18,7.25,7.11,7.38]
leave = [7.56,7.39,7.49,7.56,7.45,7.57,7.22,7.31,7.59,7.47]

mph=2.236936

# Find the non-UK plates
pattern = "(?![A-Z]{2}\d{2}\s+[A-Z]{3}$)"
foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates))

# Calculations for speed
elapsed = [(l - e)/100 for l, e in zip(leave, enter)]
speed_mps = [distance/t for t in elapsed]

def mps_to_mph():
    speed = [s*h for s,h in zip(speed_mps,mph)]

mps_to_mph()
print(speed)

错误:

>>> 
Traceback (most recent call last):
  File "M:\ICT Coursework\Task 2.1.py", line 35, in <module>
    mps_to_mph()
  File "M:\ICT Coursework\Task 2.1.py", line 33, in mps_to_mph
    speed = [s*h for s,h in zip(speed_mps,mph)]
TypeError: zip argument #2 must support iteration

也许,speed = [s*h for s,h in zip(speed_mps,mph)]不是转换速度的正确方法吗?

1 个答案:

答案 0 :(得分:4)

zip用于并行迭代两个(或更多)序列。你传递一个序列和一个数字。我想你的意思是:

speed = [s*mph for s in speed_mps]