I have a data file containing steps on each line representing each day out of the year and I want to add the appropriate amount of lines to each month.
#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')
The problem I am running into is that each month isn't sent through the appropriate if loop. So they alternate between having 31/30 days and never even checks if it is supposed to have 28 days.
CodePudding user response:
When your code does if this == that or that1 or that2:
it does a comparison of the this
and that
just like you expect it to, however it does not do the same comparison on the rest of the variables. Instead it's the same as if that1:
which is just a truthy check.
So for example:
>>> 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")
...
>>>
The correct way to compare them would look like this:
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')
- a better solution would probably be something like this:
if month in [Jan, Mar, May, ...]:
month.append(...)
...
addressing your comment: When you compare list
s in python it compares them elementwise, which is a problem when comparing two different list
s with the same contents if you want them to be treated as different.
for example:
>>> 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
A solution to this problem would be to perform an identity comparison using the id()
function, instead of comparing them directly with each other.
so something like this:
if id(month) in [id(i) for i in [Jan, Mar, Apr ...]]:
month.append(...)
...
So with all that in mind try this:
#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')