简体   繁体   English

如何将一行 python 代码分配给变量

[英]How to assign a line of python code to a variable

I am plotting in python doing something like this:我正在 python 中进行以下操作:

plt.plot(xval_a_target, q_prof_target, label=r"target", color=target_color, ls=target_style, linewidth=lwidth)

I am creating a lot of different plots this way and would like to assign the latter part to a variable:我以这种方式创建了许多不同的图,并希望将后一部分分配给一个变量:

target_plot_style = """label=r"target", color=target_color, ls=target_style, linewidth=lwidth"""

In order to shorten the plot line to: plt.plot(xval_a_target, q_prof_target, eval(target_plot_style) , I tried it with eval and exec but it does not work. Is there a simple way to do this?为了将 plot 行缩短为: plt.plot(xval_a_target, q_prof_target, eval(target_plot_style) ,我尝试使用 eval 和 exec 但它不起作用。有没有简单的方法可以做到这一点?

You can use a dict to hold those values:您可以使用 dict 来保存这些值:

kwargs = dict(label=r"target", color=target_color, ls=target_style, linewidth=lwidth)

And then apply them to the function call:然后将它们应用于 function 调用:

plt.plot(xval_a_target, q_prof_target, **kwargs)

Or you use partial to create a partially applied function:或者您使用partial创建一个部分应用的 function:

from functools import partial

p = partial(plt.plot, label=r"target", color=target_color, ls=target_style, linewidth=lwidth)
p(xval_a_target, q_prof_target)

Or you create a function:或者您创建一个 function:

def p(xval_a_target, q_prof_target):
    return plt.plot(xval_a_target, q_prof_target, label=r"target", color=target_color, ls=target_style, linewidth=lwidth)

Don't think in terms of creating source code and eval ing it on the fly.不要考虑创建源代码和eval它。

So essentially you want to have the process a bit more standardized.所以本质上你想让这个过程更加标准化。

There are two proper ways to do so:有两种正确的方法可以做到这一点:

  1. Save the parameters you want to pass additionally into a dict and pass that dict when calling:将您要另外传递的参数保存到字典中,并在调用时传递该字典:

     target_plot_style = dict(label=r"target", color=target_color, ls=target_style, linewidth=lwidth) plt.plot(xval_a_target, q_prof_target, **target_plot_style)
  2. Create a wrapper for this type of plots:为这种类型的图创建一个包装器:

     special_plot = lambda x, y: plt.plot(xval_a_target, q_prof_target, label=r"target", color=target_color, ls=target_style, linewidth=lwidth) special_plot(xval_a_target, q_prof_target)

    or maybe或者可能

    def special_plot(x, y): return plt.plot(xval_a_target, q_prof_target, label=r"target", color=target_color, ls=target_style, linewidth=lwidth) special_plot(xval_a_target, q_prof_target)

It's example how to create dict with values you need.这是如何使用您需要的值创建 dict 的示例。 Then you can add **target_plot_stype to unpack dict.然后你可以添加 **target_plot_stype 来解压 dict。

def plt_plot(xval_a_target, q_prof_target, label, color, ls, linewidth):
    pass

if __name__ == "__main__":
    target_color = target_style = lwidth = xval_a_target = q_prof_target = 'value'
    target_plot_style = dict(label=r"target", color=target_color, ls=target_style, linewidth=lwidth)
    plt_plot(xval_a_target, q_prof_target, **target_plot_style)

Given that all your properties are in a list鉴于您的所有属性都在列表中

for i in range(lwidth):

    plt.plot(xval_a_target[i], q_prof_target[i], label=r[i], color=target_color[i], ls=target_style[i], linewidth=lwidth[i])

Wouldn't this be valid enough?这还不够有效吗?

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

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