【发布时间】:2016-02-21 14:18:18
【问题描述】:
注意:我是 Python 新手。
我的任务是设计一个程序,该程序将从 10 个车牌列表(英国和外国车牌的混合)中输出 外国 车牌,但前提是它们正在超速行驶。在此过程中我犯了一些错误,我不确定如何解决这些问题。 #UK 和 #F 只是我的笔记,让我能够快速查看哪个是英国车牌,哪个是外国车牌。
import re
distance=750 #variable for the distance between the Camera A and B (in m)
speedlimit=60 # (mps)
NumberPlates=["DV61 GGB",#UK
"D3S11 EUBG 20",#F
"5T314 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.20,7.45,7.23,7.33,7.18,7.25,7.33,7.38]
#A list for the times of cars passing Camera A
Leave=[7.56,7.24,7.48,7.52,7.45,7.57,7.22,7.31,7.37,7.47]
#A list for the times of cars passing Camera B
Timestaken=[]
Timestaken2=[]
Timestaken3={}
for enter_data, leave_data in zip(Enter, Leave):
Timestaken.append(leave_data-enter_data)
Timestaken=["%.2f" % (leave_data-enter_data) for enter_data, leave_data in zip(Enter, Leave)]
Timestaken2=[s.strip("0") for s in Timestaken]
Timestaken2=[s.strip('.') for s in Timestaken2]
for key,value in zip(NumberPlates,Timestaken2):
Timestaken3[key]=value
print(Timestaken3)
for item in NumberPlates:
UK_Numbers=list(filter(lambda x: re.match("[A-Z]{2}\d{2}\s+[A-Z]{3}$",x),NumberPlates))
for item in UK_Numbers:
if item in UK_Numbers:
NumberPlates.remove(item)
print(NumberPlates) #These are foreign number plates only.
Timestaken4={}
for key,value in zip(NumberPlates,Timestaken2):
Timestaken4[key]=value
print(Timestaken4) #NumberPlate:Time
print("10 cars have passed Camera A, then Camera B\n")
for key,value in Timestaken4.items():
speed=distance/int(value)
if speed>speedlimit:
print(key,"is speeding with",distance/int(value),"mps")
我得到这个输出:
>>>
{'5T314 10A02': '28', '100 GBS 12': '04', '524 WAL 75': '24', 'D3S11 EUBG 20': '07', '24TEG 5063': '07', 'HG55 BPO': '09', 'TR44 VCZ': '04', 'TR09 TRE': '22', 'DV61 GGB': '44', 'FR52 SWD': '06'}
['D3S11 EUBG 20', '5T314 10A02', '24TEG 5063', '524 WAL 75', '100 GBS 12']
{'5T314 10A02': '07', '100 GBS 12': '22', '524 WAL 75': '07', '24TEG 5063': '28', 'D3S11 EUBG 20': '44'}
10 cars have passed Camera A, then Camera B
5T314 10A02 is speeding with 107.14285714285714 mps
524 WAL 75 is speeding with 107.14285714285714 mps
最后两条线应该有不同的速度。我意识到进入和离开时间导致的速度是不人道的,但这不是我遇到的问题。
第三个输出行显示时间分配给不同的车牌。我正在寻找解决此问题的方法。
最后两行输出与:
for key,value in Timestaken4.items():
speed=distance/int(value)
if speed>speedlimit:
print(key,"is speeding with",distance/int(value),"mps")
除了分配给不同车牌的时间之外,如何修改代码以显示正确的速度?
【问题讨论】:
标签: python python-3.x if-statement for-loop dictionary