zip 中的键和值

key and value in zip

注意:我是 Python 的新手。

我的任务是设计一个程序,输出 外国 车牌,但 如果他们超速。一路上我可能犯了一些错误,但我需要有关列表 Timestaken2 和字典 Timestaken3 的帮助。 #UK#F 只是我的笔记,让我能够快速查看哪个是 UK 车牌,哪个是 F外国.

import re

distance=750 #variable for the distance between the Camera A and B (in m)
speedlimit=70 # (mps)
NumberPlates=["DV61 GGB",#UK
              "DS11 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.15,7.24,7.45,7.28,7.31,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)

for key,value in zip(NumberPlates,Timestaken2):
    Timestaken3[key]=value

print(Timestaken3)

print("10 cars have passed Camera A, then Camera B\n")

for key,value in Timestaken3.items():
    speed=distance/int(value)
    if speed>speedlimit:
        print(key,"is speeding with",speed,"mps")

我在程序中包含了 print() 以查看程序最终执行的操作。我第二次做:

for key,value in zip(NumberPlates,Timestaken2):
    Timestaken3[key]=value

我预计只有剩余的 NumberPlates 会在这段代码之后打印出来。 Timestaken2 是否存在问题,因为它仍然有 10 个值?

请帮助我解决您可能遇到的任何问题。

不太确定,但也许你只是想制作一本新词典:

Timestaken4={}

for key,value in zip(NumberPlates,Timestaken2):
    Timestaken4[key]=value

print(Timestaken4)

问题是您在代码的第二部分重用了 TimesTaken3。如果您为超速外国汽车创建一个新字典 TimestakenForeign,您的代码似乎按预期工作:

print(NumberPlates)    

TimestakenForeign = {}

for key,value in zip(NumberPlates,Timestaken2):
    TimestakenForeign[key]=value

print(TimestakenForeign)

print("10 cars have passed Camera A, then Camera B\n")

for key,value in TimestakenForeign.items():
    speed=distance/int(value)
    if speed>speedlimit:
        print(key,"is speeding with",speed,"mps")