如何比较两个列表并找到它们之间的区别?

发布于 2025-02-11 21:39:18 字数 1240 浏览 1 评论 0原文

我正在尝试制作一个程序,该程序将打印出哪些汽车的快速发展,哪些汽车没有。输入就是这样。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2025-02-18 21:39:18

这是可以在基础Python中进行的,但是如果您正在进行更大的项目,我强烈建议您使用统计软件包(特别是numpy pandas )来执行此操作。这是因为随着数据集的增长,Numpy和Pandas以比基本Python更有效的方式存储数据。 (请参阅本文以获取更多信息)。

使用pandas

import pandas as pd

fullTime= []
file = open('traffic_input.txt').read().split('\n')
for counter, line in enumerate(file):
        data.append(line_information(line))

#create dataframe
data= pd.DataFrame(fullTime)

#split columns into starting time (sensor 1), ending time (sensor 2), and license plate
data["startingTime"] = data[fullTime].split()[1]
data["endingTime"] = data[fullTime].split()[4]
data["licensePlate"] = data[fullTime.split()[2]

#use elapsed time function to create a new column in the dataframe with elapsed time
data["elapsedTime"] = elapsed_time(data["startingTime"], data["endingTime"])

链接:

  • 有关pandas的一些有用的 href =“ https://pandas.pydata.org/docs/getting_started/intro_tutorials/05_add_columns.html” rel =“ nofollow noreferrer”>入门教程
  • a /book/“ rel =“ nofollow noreferrer”>用于数据分析书籍的Python

如果您有理由与Base Python绝对结婚,请评论,我将进行编辑。但是,对于大多数数据分析任务,Python库使您的生活更轻松(即使他们花了一点时间才能提前学习)。

编辑由于您与基本Python绑定:

我将使用带有格式的列表[[启动时间,结束时间,经过的时间] [汽车2的启动时间,CAR 2的终止时间,经过)汽车2],...],这样

timeArr = []
for i in data:
    thisRow = [];
    startingTime = i[fullTime].split()[1]
    endingTime = i[fullTime].split()[4]
    thisRow.append([startingTime, endingTime, elapsed_time(startingTime, endingTime)

之后,您可以使用使用的时间访问每个车牌的经过的时间,

for i in timeArr:
    print(i[2])

甚至可以将车牌号添加到原始数组的一部分中使用它可以访问它的一种ID。

This is possible in base python, but if you're doing a bigger project, I highly recommend using statistical packages (specifically numpy and pandas) 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:

import pandas as pd

fullTime= []
file = open('traffic_input.txt').read().split('\n')
for counter, line in enumerate(file):
        data.append(line_information(line))

#create dataframe
data= pd.DataFrame(fullTime)

#split columns into starting time (sensor 1), ending time (sensor 2), and license plate
data["startingTime"] = data[fullTime].split()[1]
data["endingTime"] = data[fullTime].split()[4]
data["licensePlate"] = data[fullTime.split()[2]

#use elapsed time function to create a new column in the dataframe with elapsed time
data["elapsedTime"] = elapsed_time(data["startingTime"], data["endingTime"])

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 so

timeArr = []
for i in data:
    thisRow = [];
    startingTime = i[fullTime].split()[1]
    endingTime = i[fullTime].split()[4]
    thisRow.append([startingTime, endingTime, elapsed_time(startingTime, endingTime)

After this, you can access the elapsed time for every license plate by using

for i in timeArr:
    print(i[2])

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文