Introductory Loop Processing

Asked 2 years ago, Updated 2 years ago, 35 views

YEAR_BEGIN=[1971, 1979, 1948]
YEAR_END=2016

VAR_list = {0:'sst', 1:'precip', 2:'air'}

for ivar in [i]:

    VAR = VAR_list [ivar]
    print('Variable', ivar, VAR)

    if VAR == 'precip':
        filei1 = DIR0 + VAR + '.mon.mean' + '.nc'   
    elif VAR == 'sst':
        filei1 = DIR0 + VAR + '.mon.mean.nc'
    elif VAR == 'air':
        filei1 = DIR0 + VAR + '.mon.mean.nc'
    else:
     pass


    clim_ref_year=1981

    if ivar = 1
        1971
    elifivar=2
        1979
    else:
        1948
timeskip=(clim_ref_year-YEAR_BEGIN[ivar])*nman-1

I want to turn the VAR_list loop and assign YEAR_BEGIN to each one, but it doesn't work.Do you have a solution?

python python3

2022-09-30 17:27

2 Answers

If you try, you will probably get an error saying 'i' is not defined in the for loop.

I think you should check the basic grammar when you turn the for loop in order with the elements in the list and rewrite the code.
(VAR_list is a dictionary, but it's also literate, so you can loop it around.)

It's not an answer, but I hope it will be an opportunity to answer.


2022-09-30 17:27

There were too many unclear points in the code provided, so I will answer only the last part of ↓.

I want to turn the VAR_list loop and assign YEAR_BEGIN to each one, but it doesn't work.Do you have a solution?

As for this, as MorningMushroom wrote, I can turn it around as for, but I think it's better because I can get the key and value at the same time by using items().

YEAR_BEGIN=[1971, 1979, 1948]
VAR_list = {0:'sst', 1:'precip', 2:'air'}

fork, vin VAR_list.items():
    clim_ref_year=YEAR_BEGIN[k]


2022-09-30 17:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.