简体   繁体   中英

How loop and store values in independent variable in python

I am very new at python, so this might sound stupid. I searched but I did not find a solution.

I have a function in python called ExcRng. What kind of for loop can I do with the function so it stores the values in independents variables? I do not wish to store them in a list, but as an independent variable.

I am looking for something like this.

EDIT* So to clarify, the inputs "a" to "n" are strings. The output df1, df2...dfn are DataFrames.

List=[a,b,c,d,e...,n]

for i in List:
    df1=ExcRange(i)
    df2=ExcRng(i)
    ...
    dfn=ExcRng(in)

You can access each element just by their index number. But if you want to have independent variables just make one venerable for one element of the List, you don't need for loop for that.

List = [a,b,c,d,e,f]

#for the first element a
df1 = ExcRange(List[0])
#for the second element b
df2 = ExcRange(List[1])
#etc List[5] = f

I would recommend you to always use list instead of multiple values because you're saving memory and your code is looking way cleaner. You can learn them, they are pretty easy

Try a dictionary:

List=[a,b,c,d,e,f]
Dict={}
for c,i in enumerate(List):
    Dict["df"+str(i+1)] = ExcRng(i)

Now, when you want to get your variable, you can use Dict["df1"]

In Python fucntions can return more than one object. You can do:

ExcRange (i) {  
 //Your code                                                            
   return df1,df2,,,,,,,,dfn
}

In Your main Code: for i in List df1,df2,,,,dfn= ExcRange (i)

It's not very usual to do what you're trying, so I recommend you take some time to learn about dictionaries and other data structures that might help you. Said that, I see a way of achieving something like this:

The python method exec allows dynamic execution of python code, which in our case means running a line of code from a string.

So, if you'd normally use a = 1 to declare/attribute a variable named a with value 1 , you can achieve the same result with exec("a = 1") .

This allows you to useformatted strings to create variables from a list of variable names given as strings:

var_names = ["a", "b", "c", "d"]

for var in var_names:
  exec("{} = {}".format(var, var_names.index(var)*2))

print(a)
print(b)
print(c)
print(d)

Once we're attributing to the variable the index of the name in the list multiplied by two, this should be the output:

0
2
4
6

It wasn't exactly clear to me what you want to do, so reading again the question I got the impression you might want the values returned by the ExcRange method to be stored on separate variables, not on a list, and if that's the case, good news: python syntax sugar is here to help!

def method():
    return 1, 3, 5, 7

a, b, c, d = method()

print(a) # a = 1
print(b) # b = 3
print(c) # c = 5
print(d) # d = 7

On this case, the method returns a list with 4 elements [1,3,5,7] , but if you assign the result to exactly 4 variables, Python unpacks it for you.

Note that if you assign the method to a single variable, this variable will store the list itself.

a, b, c, d = method()

print(a) # a = [1, 3, 5, 7]
values = [a,b,c,d,e...,n]
dfs = {f'df{i+1}': ExcRng(v) for i, v in enumerate(values)}
print(dfs['df1'])
...
<Your dataframe here>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM