Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

python - Subtraction of two items from two lists in for command

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...