954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Referring to items in a list

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

spe_eddy
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
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:]
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

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

spe_eddy
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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:]))
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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

spe_eddy
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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
richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

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

spe_eddy
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
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.

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

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!

spe_eddy
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: