Repeat to call variables

0

I have four variables [t1, t2, t3, t4], and each of them has been defined as a string before.

t1 = 'A vida vai ficando cada vez mais dura perto do topo.'
t2 = 'A moralidade é a melhor de todas as regras para orientar a humanidade.'
t3 = 'Aquilo que se faz por amor está sempre além do bem e do mal.'
t4 = 'Torna-te aquilo que és.'

If in the code, I want to call each of these variables at once, I thought of doing this:

for i in range(1,5):
   print(ti)

Thus, i would be replaced by the numbers 1 to 4 and then call t1, t2, t3, t4 subsequently. Obviously, this method does not work. In fact it is possible to create a list with the texts and then do:

lista = [t1, t2, t3, t4]
for i in range(1,5):
   print(lista[i])

But it becomes antipractical to create a list when the number of variables is too high. My question is: is there any way to call multiple pre-defined variables at once, how would it work in the above 'for' repetition above?

    
asked by anonymous 02.04.2018 / 00:12

4 answers

2

In Python, in general, when you want to use variable_names as variables per se, the recommendation is to use a dictionary.

That is, instead of having "f1, f2, f3, f4", you could have a dictionary f with the keys 1, 2, 3 ,4 .

But of course there are other ways - in case you just want to avoid having to repeat 4 times the same line of code. What you're not taking into account is that for of Python always traverses sequences, and there are several forms of sequences - not just the numeric sequence returned by range .

In case, all you need is:

for var in (f1, f2, f3, f4):
    print(var)

Ready: You create a local tuple type sequence with the desired variables, and each tuple element is associated with the for variable - and you avoid having to repeat print 4 times.

Do not do this But I'll include it for reference: although you should not do this: the Python variables themselves are stored internally in dictionary-like structures. Then it is possible to access variables programmatically by name by accessing those dictionaries that are returned by calling the internal functions globals() and locals() , respectively for global and local variables.

for i in range(1, 5):
    print(globals()[f"f{i}"])
    
02.04.2018 / 16:05
1

One option is to use 'dict comprehensions'. Change the range value and increase the number of variables:

d = {'f' + str(i): 0 for i in range(5)}

Produces the following output:

{'f0': 0, 'f1': 0, 'f2': 0, 'f3': 0, 'f4': 0}

And to view:

for k, v in d.items():
    print(k, v)

Produce:

f0 0
f1 0
f2 0
f3 0
f4 0
    
02.04.2018 / 02:03
0

I do not know if I understand completely, but here is a possible answer to your problem: If you have a list, such as:

lista = [f1,f2,f3,f4]

You can use the list directly in the for:

for i in lista:
    print(i)

The output will be:

f1
f2
f3
f4

If you want the same result, but with the counter, do:

for cont, i in enumerate(lista):
    print(cont)
    print(i)

the result will be:

0
1
2
3
f1
f2
f3
f4

So we can solve your problem using one of these methods, mentioned above. If you want to create a vector with numbers, do:

lista2 = [] #lista vazia
for i in range(5):
    lista2.append(i)
    print(lista2)

The result will be:

[0,1,2,3,4]

I hope I have helped, if it has not been answered, please re-write your question. vlw!

    
02.04.2018 / 02:32
0

I'm not sure I understand your question, but from what I understand you could use a dictionary, like this:

d = {'f1':1,'f2':2,'f3':3,'f4':4}
for x in range(1,5):
    print('{0} => {1}'.format('f'+str(x), d['f'+str(x)]))

and print according to your wishes. When you were to add new values you could do:

d['f'+str(int(max(d.keys())[1])+1)] = input()
    
02.04.2018 / 03:33