简体   繁体   中英

Create a lengthy list of variable names in python with for loop

I have a bunch of data frames whose names are Actuals_17, Actuals_18,...Actuals45 . I would like to create a list of these variables. I know that it can be created by manually typing:

varlist = [Actuals_17,Actuals_18,Actuals19,Actuals20]

However, it would not result in a neat code if the number of variables are large. Is there a method to make the varlist with a for loop?

You can use python built-in globals() function which returns a dictionary representing the current global symbol table.

varlist = [globals()[f'Actuals_{i}'] for i in range(17, 46)]

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