简体   繁体   English

具有变量赋值的 Python Map 或 Filter 函数

[英]Python Map or Filter function with variable assignment

Is there a way to use either of the map or filter functions to map a list of variables to a function and assign a variable (the name of the variable) to each of those outputs?有没有办法使用 map 或 filter 函数将变量列表映射到函数并将变量(变量的名称)分配给每个输出?

    mapping_tables = ['Assets_Table','Liabilities_Table','Equity_Table','Income_Table']

I have a list of tables that I'm applying to a function which will convert it into a dataframe and then assign a variable to the dataframe from the list of mapping_tables.我有一个表列表,我将其应用于一个函数,该函数会将其转换为数据框,然后从 mapping_tables 列表中为数据框分配一个变量。 At the moment the following 'for loop' works.目前,以下“for循环”有效。

for table_name in mapping_tables:
    globals()[table_name] = load_data_table_to_df(sheet_name, table_name)

Is there a way to optimize this?有没有办法优化这个? Perhaps using one of the Map or Filter functions?也许使用 Map 或 Filter 功能之一? or even a different python library?甚至是不同的python库? Or is this as simple as it gets?或者这很简单?

Thank you谢谢

If you really want to use map() :如果你真的想使用map()

# Assuming `sheet_name` is defined somewhere...
def table_to_df(table_name):
    return table_name, load_data_to_df(sheet_name, table_name)


frames = dict(map(table_to_df, mapping_tables))

Then when you want to work on one of your dataframes:然后,当您想处理其中一个数据框时:

frames[table_name]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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