Python,如何连接2个变量并创建指向第3个变量的名称

时间:2018-09-02 11:22:19

标签: python automation

我想通过在if语句中将2个变量(如果打印出来)创建指向第三个变量的名称,来减少代码并消除3条if语句。

目前,我有3条if语句,如下所示:

if prd.lower() not in retentions_log:

我想使用类似的东西:

if prd.lower() not in retentions_+prd.lower:

因此,每次调用该函数都会构造第三个变量

  

retentions + prd.lower = retentnions_log

这是功能之一:

retentinon_approval_log = "SOME_VALUE"

def ret_handler_log(roletype, prd, pth, fnm, location):#                    [retention_proc]
    """Processors. 4Writing results to file."""
    if prd in retentinon_approval_log:
        if approval_check(roletype, prd, pth) == False:
            try:
                results_o.write("some_text")
            except Exception as e:
                print(e)
                pass
        else:
            yaml_dumper(roletype, prd, location)
            results_o.write("some_text")
    else:
        yaml_dumper(roletype, prd, location)
  

更新,我希望能够构建“ retentinon_approval_ + prd”   保留批准是简单的文本,而prd是“ log”。至   构造一个变量“ retentinon_approval_log”,该变量指向“一些   文字”,并使用不同的值动态生成   传递给该功能

1 个答案:

答案 0 :(得分:1)

您可以使用字典来构建变量。例子。

class Program
{
    const int DATA_SIZE = 10000;
    const int IENUMERABLE_COUNT = 10000;

    static void Main(string[] args)
    {
        // initialisation - takes milliseconds
        IEnumerable<string>[] iEnumerablesArray = GenerateArrayOfIEnumerables();

        Console.WriteLine("Initialized");

        List<string> result = null;

        // =================
        // THE PROBLEM LINE:
        // =================
        // CPU usage of next line:
        //    - 40 % on 4 virtual cores processor (2 physical)
        //    - 10 - 15 % on 12 virtual cores processor
        Parallel.For(
            0, 
            int.MaxValue, 
            (i) => result = iEnumerablesArray.Aggregate(Enumerable.Concat).ToList());

        // just to be sure that Release mode would not omit some lines:
        Console.WriteLine(result);
    }

    static IEnumerable<string>[] GenerateArrayOfIEnumerables()
    {
        return Enumerable
              .Range(0, IENUMERABLE_COUNT)
              .Select(_ => Enumerable.Range(0, 1).Select(__ => GenerateLongString()))
              .ToArray();
    }

    static string GenerateLongString()
    {
        return string.Concat(Enumerable.Range(0, DATA_SIZE).Select(_ => "string_part"));
    }
}