从命令中的两个列表中减去两个项目 [英] Subtraction of two items from two lists in for command

查看:168
本文介绍了从命令中的两个列表中减去两个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个任务来创建一个程序,捕捉外国的车牌号码,正在超速。我已经完成了进入和离开的时间,后来被分配到10个不同的号码牌(这一点不重要)。

为了节省空间,我试图在Leave和Enter列表中使用for命令,希望从Leave项目中拿走Enter的项目,以获取在程序中从一个摄像机到另一个摄像机所花费的时间 。 / p>

我怎样才能有效地做到这一点?这是我尝试的东西,但我知道为什么它是错的,我无法找到一个解决方案。



import itertoolsEnter = [7.12,7.14, 7.24,7.45,7.28,7.31,7.18,7.25,7.33,7.38]#汽车通过时间列表照相机ALeave = [7.56,7.24,7.48,7.57,7.35,7.57,7.27,7.31,7.37,7.41]#一个汽车通过时间的列表相机BTimestaken = []为itertools.chain中的项目(离开,回车):Timestaken.append(item-item)print(Timestaken)

我得到的结果是这样的,绝对是因为for命令仍然将一个列表的项目从同一个项目中移走???:



 >>> [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 

解决方案

我想你正在寻找 zip

  Enter = [7.12,
7.14,
7.24,
7.45,
7.28,
7.31,
7.18,
7.25,
7.33,
7.38]#汽车时间表经过相机A

离开= [7.56,
7.24,
7.48,
7.52,
7.45,
7.57,
7.22,
7.31,
7.37,
7.41]#汽车通过摄像机B

的enter_data,leave_data的zip(回车,离开) :
print(leave_data - enter_data)


I'm fairly new to Python and I still do basic stuff for my ICT course.

I have a task to create a program that catches foreign number plates that are speeding. I have done times of entry and leave, which would be later assigned to 10 different number plates (that bit isn't important).

To save space, I'm trying to use the for command on the Leave and Enter lists hoping to take away Enter's item from Leave's item, to get the time it took for a car to get from Camera A to B in the program.

How can I do it effectively? Here's something I tried, although I know why it's wrong, I can't find a solution anywhere.

import itertools

Enter=[7.12,
       7.14,
       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.41] #A list for the times of cars passing Camera B

Timestaken=[]

for item in itertools.chain(Leave,Enter):
    Timestaken.append(item-item)

print(Timestaken)

The result I get is this, definitely because the for command still takes one list's item away from the same item???:

>>> 
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

解决方案

I think you're looking for zip

Enter=[7.12,
       7.14,
       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.41] #A list for the times of cars passing Camera B

for enter_data, leave_data in zip(Enter, Leave):
    print(leave_data - enter_data)

这篇关于从命令中的两个列表中减去两个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆