简体   繁体   English

更改 python 代码以摆脱 exec() 和 eval()

[英]Altering python code to get rid of exec() and eval()

I am new to python and am trying to improve a code's readability as well as speed by removing the recurrent use of exec() and eval().我是 python 的新手,正在尝试通过删除 exec() 和 eval() 的重复使用来提高代码的可读性和速度。 However it is not obvious to me how I need to alter to code to obtain this.但是,我不清楚我需要如何更改代码才能获得它。

I want the program to make dataframes and arrays with names based on input.我希望程序使用基于输入的名称制作数据帧和 arrays。 Let's say that the input is like this:假设输入是这样的:

A=[Red, Blue]
B=[Banana, Apple]
C=[Pie, Cake]

Then the code will then make a dataframe with a name based on each combination of input: Red_Banana_Pie, Red_Banana_Cake, Red_Apple_Pie, Red_Apple_Cake, etc. by looping through the three lists.然后代码将通过循环遍历三个列表,根据输入的每种组合创建一个名称为 dataframe 的名称:Red_Banana_Pie、Red_Banana_Cake、Red_Apple_Pie、Red_Apple_Cake 等。

for color in A[0:len(A)]: 
    for fruit in B[0:len(B)]: 
        for type in C[0:len(C)]: 

And then in each loop:然后在每个循环中:

exec('DataFr_'+color+'_'+fruit+'_'+type+'=pd.DataFrame((Data),columns=[\'Title1\',\'Title2\'])')

How can I do this without the exec command?如果没有 exec 命令,我怎么能做到这一点?

When you run exec('DataFr_'+color+'_'+fruit+'_'+type+'=pd.DataFrame((Data),columns=[\'Title1\',\'Title2\'])') , you will get 8 DataFrames that has different names.当你运行exec('DataFr_'+color+'_'+fruit+'_'+type+'=pd.DataFrame((Data),columns=[\'Title1\',\'Title2\'])')时,你将得到 8 个具有不同名称的 DataFrame。 But I don't recommend this because you have to use eval() every time you want to access to your DataFrame.(otherwise you can hardcode that, but that's really bad thing)但我不推荐这样做,因为每次你想访问你的 DataFrame 时你都必须使用eval() 。(否则你可以硬编码,但这真的很糟糕)

I think you need multi-dimensional dictionary for dataframe.我认为您需要 dataframe 的多维字典。

When the input is当输入是

A=["Red", "Blue"]
B=["Banana", "Apple"]
C=["Pie", "Cake"]

[+] additionally, you'll basically get user input as string in python.(like "hello, world!" ) [+] 此外,您基本上会在 python 中以string形式获得用户输入。(例如"hello, world!"

data_set = {}
for color in A:
    data_set.update({color:{}})
    for fruit in B: 
        data_set[color].update({fruit:{}})
        for type in C:
            data_set[color][fruit].update({type:pd.DataFrame((Data),columns=['Title1','Title2'])})
                                                     # I think you have some Data in other place, right?

[+] moreover, you can iterate List without [0:len(A)] in python. [+] 此外,您可以在 python 中迭代没有[0:len(A)]的列表。

Then you can use each DataFrame by data_set['Red']['Banana']['Cake'] .(your implementation will be data_set[A[0]][B[0]][C[1]] ) Then you can dynamically create DataFrame for each color, fruit, type without eval, and access to them without hardcoded value.然后你可以通过data_set['Red']['Banana']['Cake']使用每个 DataFrame 。(你的实现将是data_set[A[0]][B[0]][C[1]] )然后您可以为每种颜色、水果、类型动态创建 DataFrame,无需评估,无需硬编码值即可访问它们。

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

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