-3

I have a list that looks something like this: weather_history=((year,month,day),precip,tmin,tmax)

I need to split it into one-year chunks where each chunk is a list with one years worth of data please help!

    all_years_data: List[Weather_List] = []
    
    for line,p,n,x in weather_history:
            
        year=line[0]
        day=line[2]
        month=line[1]
        precip=p
        tmin=n
        tmax=x

        if year not in all_years_data:
            all_years_data.append(year)
   

this is my code so far. I've tried many different things to get all of each years worth of data into one list but can't figure it out

  • you need something like [ [year,day,month,tmin,tmax],[ [year,day,month,tmin,tmax] ] ? List inside list ? https://stackoverflow.com/questions/18980396/building-a-list-inside-a-list-in-python – Tharanga Abeyseela Feb 15 '23 at 01:01

2 Answers2

0

How about this?

A = [((1,2,3),4,5,6), ((10,20,30),40,50,60), ((100,200,300),400,500,600)]

B = [i[0][0] for i in A]
0

If your data is like this:

weather_history = ((2020,6,12),30,10,40)

you can use weather_history index without for statement :

year = weather_history[0][0]
day = weather_history[0][1]
month = weather_history[0][2]
precip = weather_history[1]
tmin = weather_history[2]
tmax = weather_history[3]
if year not in all_years_data:
    all_years_data.append(year)

But if your data is like this:

weather_history = [((2020,6,12),30,10,40),((2021,6,12),30,10,40),((2022,6,12),30,10,40)]

you should loop weather_history data with for statement :

for line in weather_history:
  year = line[0][0]
  day = line[0][1]
  month = line[0][2]
  precip = line[1]
  tmin = line[2]
  tmax = line[3]
  if year not in all_years_data:
      all_years_data.append(year)

Jordy
  • 1,802
  • 2
  • 6
  • 25