如何比较两个列表并找到它们之间的区别?
我正在尝试制作一个程序,该程序将打印出哪些汽车的快速发展,哪些汽车没有。输入就是这样。 1 12:00:03 DX-89-EH 2 12.00.42 DX-89-EH 此列表由大量的数字板组成,因此我想将它们分为两组。一个用于传感器一,另一个用于传感器两个。之后,我想比较时间,并计算它们之间的差异。然后,我可以计算最终速度。到目前为止,我随之而来的是:
distance = 200
def speed(line):
max_speed = line
print(max_speed)
def line_information(line):
sensor, time, plate = line.split()
return {"sensor": sensor, "time": time, "plate": plate}
data = []
file = open('traffic_input.txt').read().split('\n')
for counter, line in enumerate(file):
if counter == 0:
speed(line)
else:
data.append(line_information(line))
def elapsed_time(time_start, time_end):
hours, minutes, seconds = time_start.split(":")
time_start_sec = int(hours) * 3600 + int(minutes) * 60 + int(seconds)
hours, minutes, seconds = time_end.split(":")
time_end_sec = int(hours) * 3600 + int(minutes) * 60 + int(seconds)
elapsed_time = time_end_sec - time_start_sec
def speed(kmh):
actual_speed = (distance / elapsed_time) * 3.6
file = open('traffic_input.txt').read().split('\n')
for counter, line in enumerate(file):
if counter == 0:
speed(line)
else:
line_information(line)
有人可以解释一下我可以做什么来完成这项工作,以及如何比较列表? 谢谢。
I am trying to make a program which will print which cars went to fast, and which cars didn't. The input is like this.
1 12:00:03 DX-89-EH
2 12.00.42 DX-89-EH
This list consists of a very large number of number plates, so I want to split these in two groups. One for sensor one, and one for sensor two. Afterwards I want to compare the times, and calculate the difference between them. I can then calculate the final speed. Thus far I came with this:
distance = 200
def speed(line):
max_speed = line
print(max_speed)
def line_information(line):
sensor, time, plate = line.split()
return {"sensor": sensor, "time": time, "plate": plate}
data = []
file = open('traffic_input.txt').read().split('\n')
for counter, line in enumerate(file):
if counter == 0:
speed(line)
else:
data.append(line_information(line))
def elapsed_time(time_start, time_end):
hours, minutes, seconds = time_start.split(":")
time_start_sec = int(hours) * 3600 + int(minutes) * 60 + int(seconds)
hours, minutes, seconds = time_end.split(":")
time_end_sec = int(hours) * 3600 + int(minutes) * 60 + int(seconds)
elapsed_time = time_end_sec - time_start_sec
def speed(kmh):
actual_speed = (distance / elapsed_time) * 3.6
file = open('traffic_input.txt').read().split('\n')
for counter, line in enumerate(file):
if counter == 0:
speed(line)
else:
line_information(line)
Can someone please explain what I can do to make this work, and how I can compare the lists?
Thanks in regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可以在基础Python中进行的,但是如果您正在进行更大的项目,我强烈建议您使用统计软件包(特别是
numpy
pandas )来执行此操作。这是因为随着数据集的增长,Numpy和Pandas以比基本Python更有效的方式存储数据。 (请参阅本文以获取更多信息)。使用
pandas
:链接:
如果您有理由与Base Python绝对结婚,请评论,我将进行编辑。但是,对于大多数数据分析任务,Python库使您的生活更轻松(即使他们花了一点时间才能提前学习)。
编辑由于您与基本Python绑定:
我将使用带有格式的列表
[[启动时间,结束时间,经过的时间] [汽车2的启动时间,CAR 2的终止时间,经过)汽车2],...]
,这样之后,您可以使用使用的时间访问每个车牌的经过的时间,
甚至可以将车牌号添加到原始数组的一部分中使用它可以访问它的一种ID。
This is possible in base python, but if you're doing a bigger project, I highly recommend using statistical packages (specifically
numpy
andpandas
) to do this. This is because as your dataset grows, numpy and pandas store data in a much more efficient way than base python. (see this article for more information).Using
pandas
:Some helpful links about pandas:
If you have a reason to be absolutely married to base python, comment and I'll edit. But for most data analysis tasks, python libraries make your life way easier (even if they take a little bit of time to learn up front).
Edit since you're tied to base python:
I would approach this by using a list of lists with the format
[[starting time, endingtime, elapsed time][starting time for car 2, ending time for car 2, elapsed time for car 2], ...]
, like soAfter this, you can access the elapsed time for every license plate by using
You could even add the license plate number to part of the original array if you wanted some kind of ID to access it with.