Cesar
Cesar

Reputation: 585

Loop in python - Repeat sentence, changing only the number inside

I have a sequence :

    ['Valor','005 - 001']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 001');
    ['Valor','007 - 001']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 001');
    ['Valor','019 - 001']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 001');
    ['Valor','024 - 001']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 001');


    ['Valor','005 - 002']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 002');
    ['Valor','007 - 002']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 002');
    ['Valor','019 - 002']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 002');
    ['Valor','024 - 002']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 002');


    ['Valor','005 - 003']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 003');
    ['Valor','007 - 003']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 003');
    ['Valor','019 - 003']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 003');
    ['Valor','024 - 003']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 003');

....

Where the only thing that changes on each line is the number after the hyphen :

['Valor','005 - 001']=>DB('Cubo_Amostra_Cons',!Ano,'000 - 001');

How can I loop this value from "001" through "030"?

I have tried:

for x in range(001,030):
print ( "['Valor','005 -" x"']=>DB('Cubo_Amostra_Cons',!Ano,'000 - "x"');
    ['Valor','007 - "x"']=>DB('Cubo_Amostra_Cons',!Ano,'000 - "x"');
    ['Valor','019 - "x"']=>DB('Cubo_Amostra_Cons',!Ano,'000 - "x"');
    ['Valor','024 - "x"']=>DB('Cubo_Amostra_Cons',!Ano,'000 - "x"');")

But I didn't get the right sequence.

Upvotes: 0

Views: 248

Answers (2)

Prune
Prune

Reputation: 77837

I believe that you want to format the value string:

for x in range(20, 30):
    x_str = str(x).zfill(3)

... and use that in your output. Also, you can parameterize your four variations:

class = [5, 7, 19, 24]
for post in range(20, 30):
    post_str = str(post).zfill(3)
    for pre in class:
        pre_str = str(pre).zfill(3)
        valor_str = pre_str + " - " + post_str
        cons_str  = "000 - " + post_str
        # Here, valor_str in the first label you want;
        #        cons_str is the last.

Can you finish from there?

Note that the final value listed in range isn't used; I believe you want

for post in range(1, 31):

Upvotes: 1

Ashlou
Ashlou

Reputation: 694

python is an array-based language and can be vectorized. Please use vectors (arrays) instead of for loop in order to have a better performance.

Upvotes: 0

Related Questions