简体   繁体   English

我如何使我的代码在python中的for循环中运行

[英]How do I make my code run in a for loop in python

I wanted to make this kinda code run in for loop rather me putting 3,6,9 and so on and similarly for total cars ie 2,3,4. 我想让这种代码在for循环中运行,而不是我输入3,6,9,依此类推,总车数也是如此,例如2,3,4。 Because what I'm doing does not seem to be a good approach. 因为我在做的事情似乎不是一个好方法。

Code: 码:

 data[(( data.total_people > 3 )& (data.total_cars < 2 )) | (( data.total_people > 6 )& (data.total_cars < 3 )) ]

Data: 数据:

 total_people   total_cars
2   4.0         1
7   5.0         1
19  4.0         1
21  6.0         1
40  4.0         1
43  4.0         1
48  4.0         1

Data is about people are more than 3 and car is only one. 关于人的数据超过3,而汽车仅为1。 people are more than 6 and car is only 2 and so on for 9,12. 人大于6,而汽车仅为2,依此类推,为9,12。 I thought about adding 3 to total people like 3,6,9,12 and 1 each time to cars but how i'll do this in for loop. 我考虑过将3乘以3、6、9、12和1来添加到汽车中,但我将如何在for loop中做到这一点。

I think you should clarify a bit. 我想你应该澄清一下。 Is this what you had in mind? 这就是您的想法吗?

for i in range(0, n):
    data[(( data.total_people > 3*(i+1) )& (data.total_cars == i+1 ))]    
    #process your data here

Where n is how many times you want to check n是您要检查多少次

n = 1 => 3 people, 1 car n = 1 => 3人,1辆车

n = 2 => 6 people, 2 cars n = 2 => 6人,2辆车

n = 3 => 9 people, 3 cars etc n = 3 => 9人,3辆车,等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM