我有一个数据文件,每一行包含一年中每一天的步骤,我想在每个月添加适当数量的行.

 #Initilize lists
listOfSteps = []
Jan = []
Feb = []
Mar = []
Apr = []
May = []
Jun = []
Jul = []
Aug = []
Sept = []
Oct = []
Nov = []
Dec = []

# Open file into program and insert data into list called listOfSteps
step_file = open('/Users/gregoryyelverton/Desktop/Data files/steps.txt', 'r')

for line in step_file:
        line = line.rstrip('\n')
        listOfSteps.append(line)



# Create list with all months inside
months = [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]

# Iterate through list containing months and insert
# the days within those months

for month in months:
    #print(month)
    if month == [Jan] or [Mar] or [May] or [Jul] or [Aug] or [Oct] or [Dec]:
        month.append(listOfSteps[:31])
        del listOfSteps[:31]
        print('31 days')

    if month == [Apr] or [Jun] or [Sept] or [Nov]:
        month.append(listOfSteps[:30])
        del listOfSteps[:30]
        print('30 days')

    else:    
        month.append(listOfSteps[:28])
        del listOfSteps[:28]
        print('28 days')

我遇到的问题是,每个月都没有通过适当的if循环发送.因此,他们交替 Select 31/30天,甚至从不判断是否应该有28天.

推荐答案

当代码执行if this == that or that1 or that2:时,它会像预期的那样对thisthat进行比较,但是它不会对其余变量进行相同的比较.相反,它和if that1:一样,只是一张真实的支票.

例如:

>>> a = 1
>>> b = 2
>>> c = 3
>>> if a == b or c:
...     print("This code runs even though a does not equal b or c")
...
This code runs even though a does not equal b or c
>>> if a == b or a == c:
...     print("this will not print because a does not equal b or c")
...
>>> 

比较它们的正确方法如下所示:

if month == Jan or month == Mar or month == May or month == Jul or month == Aug or month == Oct or month == Dec:
    month.append(listOfSteps[:31])
    del listOfSteps[:31]
    print('31 days')
  • 更好的解决方案可能是这样的:
if month in [Jan, Mar, May, ...]:
    month.append(...)
    ...

回应您的 comments :当您在python中比较list时,它会对它们进行元素级的比较,如果您希望将两个不同的list与相同的内容进行比较,这是一个问题.

例如:

>>> a = [1]
>>> b = [1]
>>> c = a
>>> if c == b:
...    print("The contents are the same so this will print")
The contents are the same so this will print

这个问题的解决方案是使用id()函数执行身份比较,而不是直接相互比较.

比如:

if id(month) in [id(i) for i in [Jan, Mar, Apr ...]]:
    month.append(...)
    ...

因此,考虑到所有这些,try 以下方法:

#Initilize lists
listOfSteps = []
Jan = []
Feb = []
Mar = []
Apr = []
May = []
Jun = []
Jul = []
Aug = []
Sept = []
Oct = []
Nov = []
Dec = []

# Open file into program and insert data into list called listOfSteps
step_file = open('/Users/gregoryyelverton/Desktop/Data files/steps.txt', 'r')

for line in step_file:
        line = line.rstrip('\n')
        listOfSteps.append(line)

# Create list with all months inside
months = [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]

# Iterate through list containing months and insert the days within those months

for month in months:
    #print(month)
    if id(month) in [id(i) for i in [Jan,Mar,May,Jul,Aug,Oct,Dec]]:
        month.append(listOfSteps[:31])
        del listOfSteps[:31]
        print('31 days')

    elif id(month) in [id(i) for i in [Apr,Jun,Sept,Nov]]:
        month.append(listOfSteps[:30])
        del listOfSteps[:30]
        print('30 days')

    else:    
        month.append(listOfSteps[:28])
        del listOfSteps[:28]
        print('28 days')

Python相关问答推荐

如何列举Pandigital Prime Set

大小为M的第N位_计数(或人口计数)的公式

基于字符串匹配条件合并两个帧

对所有子图应用相同的轴格式

Pandas—合并数据帧,在公共列上保留非空值,在另一列上保留平均值

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

创建可序列化数据模型的最佳方法

Tkinter菜单自发添加额外项目

使用BeautifulSoup抓取所有链接

导入错误:无法导入名称';操作';

干燥化与列姆化的比较

巨 Python :逆向猜谜游戏

递归函数修饰器

mdates定位器在图表中显示不存在的时间间隔

Tensorflow tokenizer问题.num_words到底做了什么?

numpy数组和数组标量之间的不同行为

如何在Python中从html页面中提取html链接?

无法使用请求模块从网页上抓取一些产品的名称

具有数值数组问题的递归矩阵构造(广播?)

在极坐标中添加列总计行