简体   繁体   English

如何遍历列表?

[英]How can I iterate through a list?

I'm super new to Python, I want to do a simple loop that iterates through a list which has the name of the months: 我是Python的新手,我想做一个简单的循环,遍历列表中的月份名称:

I tried For loops like: 我尝试了For循环,例如:

months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
Max=[]
hours=[]
for m in months:
    time=m.count()/96  # when it goes to "Jan" it has 2976 elements
    for i in range(1,int(time)+1): 
        a=Rdata.m[Rdata.m['Day'] == i].dem.max() # Rdata.Jan is a df which has columsn Day and dem
        b=Rdata.m.loc[Rdata.m['dem']== a,'Time']
    Max.append(a)
    hours.append(b)

Just to add more information 只是添加更多信息

I created a list : 我创建了一个列表:

month= (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)

in which 在其中

Jan = Series which contains  2976 elements

And I keep getting the error "count() takes at least 1 argument (0 given)" 而且我不断收到错误消息“ count()至少接受1个参数(给定0)”

Count is used for counting the number of elements with the specified value.for example: fruits = ['apple', 'banana', 'cherry'] Count用于对具有指定值的元素数进行计数。例如:fruits = ['apple','banana','cherry']

x = fruits.count("cherry") x = fruit.count(“ cherry”)

You should be using len() instead of count there 您应该使用len()而不是在那里计数

time=len(m)/96 时间= len(m)/ 96

When you enclose the values in double quotes or single quotes they are literal strings not series variables. 当您将值括在双引号或单引号中时,它们是文字字符串,而不是序列变量。

Assuming that your code has defined the series from Jan to Dec, you need to create the list months of the series like this: 假设您的代码定义了从1月到12月的系列,则需要创建该系列的列表月份,如下所示:

months=[Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec]

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

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