I am wanting to refer to a number of items from the end of a list, but can't get the syntax right,

previousDays = 5
pDaysRange = (31-previousDays,32)

previousDays is the variable I can change,
there are 31 days in the month so i'm trying to take the last 5 days in this case,

trainingDays = trainingData.getMeasurements()

for day in trainingDays:

       for i in range(pDaysRange,32):
		if day[i].condition == "Fog":
			prevDaysF += 0.5
		if day[i].condition == "Snow":
			prevDaysS += 0.5
		if day[i].condition == "Rain":
			prevDaysR += 0.5
		if day[i].condition == "None":
			prevDaysN += 0.5
		print prevDaysF

I know range doesn't work with anything but an integer, but i'm just illustrating what i need to happen,

this code will repeat for each of the other 4 days,

i can't repeat the code either as the number i'm implementing prevDays with is different with each day

I think i need to define the range similar to

for day in trainingDays

, but i'm not sure how to do this

there are 31 days in the month so i'm trying to take the last 5 days in this case

That would be something along the lines of:

print range(32)[-5:]

oh sorry I didn't mean to leave the print line there, that is helpful but it's not what i want to do! I want to use elements from a list, so using range with trainindays, rather than a range of integers

Is this helpfull?

import random
test_data = [random.randint(28,31) for count in range(12)]
day_data = [[random.randint(1,5)
                  for count in range(days)]
                  for days in test_data ]
print('Data generated')
for m, month in enumerate(day_data):
    print(m+1, month)

n = 5
for m, month in enumerate(day_data):
    print('Last %i days measurements for month %i are:\n%s' % (n, m+1, month[-n:]))

Sorry not, I amended my code after the help from woooee:

for day in testingDays[-5:]:
	if day[1].condition == "Fog":
		prevDaysF += 0.2
	if day[1].condition == "Snow":
		prevDaysS += 0.2
	if day[1].condition == "Rain":
		prevDaysR += 0.2
	if day[2].condition == "Fog":
		prevDaysS += 0.2
	if day[2].condition == "Snow":
		prevDaysR += 0.2
	if day[2].condition == "Rain":

But i can't refer to the days as this as it returns an error

Always do a logical known check first

trainingDays = trainingData.getMeasurements()

for i in range(pDaysRange,32):

       for day in trainingDays:
		if day[i].condition == "Fog":
			prevDaysF += 0.5
		if day[i].condition == "Snow":
			prevDaysS += 0.5
		if day[i].condition == "Rain":
			prevDaysR += 0.5
		if day[i].condition == "None":
			prevDaysN += 0.5
		print prevDaysF

sorry i don't know what you've done there, there's still a tuple for range which it doesn't like, i got closer on my more recent post

Edited 5 Years Ago by spe_eddy: n/a

sorry i don't know what you've done there, there's still a tuple for range which it doesn't like, i got closer on my more recent post

Look i dont think you really need a help.
If you vote people down. If you can do your stuff, you dont ask it here period. also i gave you an idea about how checks should be done so that you can understand how its done. No one is here to do your job for you. We are here to help and you must be very thankful for that.

Edited 5 Years Ago by richieking: n/a

i voted you down because i thought you posted the same code! Then i couldn't change it back,
i said that because it looked like you ignored all the other posts except the first one,
i don't understand what you meant about how checks should be done,
and i know, i am thankful!

This article has been dead for over six months. Start a new discussion instead.