将变量附加到变量中

时间:2016-02-10 18:13:22

标签: python

我对python有点新意。这可能是一个简单的问题,但不得不问。非常感谢。

这就是我在下面的代码中所做的。

sysLst = str(raw_input('Enter pipe separated list of items [PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|SCALE|ALL]: ')).upper().strip()
nsysLst1="|".join(["^" + sysList_t + "$" for sysList_t in sysLst.split('|')])
nsysLst = nsysLst1.replace("^SCALE$", "PRIMARY-SCALE1|PRIMARY-SCALE2|AUXVM-SCALE1|AUXVM-SCALE2")
print nsysLst
if "ALL" in nsysLst:
        nsysLst = "PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|SCALE"

当我运行上面的代码并将输入作为PRIMARY时,我得到的结果为:

Enter pipe separated list of items [PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|SCALE|ALL]: PRIMARY
^PRIMARY$

如果输入为SCALE,我会得到输出:

Enter pipe separated list of items [PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|SCALE|ALL]: SCALE
PRIMARY-SCALE1|PRIMARY-SCALE2|AUXVM-SCALE1|AUXVM-SCALE2

现在我的问题是当我提供输入时所有'所有'如何更改' SCALE'使用  nsysLst'输出的选项。

到目前为止,我在提供' ALL'输入:

Enter pipe separated list of items [PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|SCALE|ALL]: ALL
^ALL$

但我的所有'所有'输入我的预期输出应该是:

Enter pipe separated list of items [PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|SCALE|ALL]: ALL
^PRIMARY$|^PRIMARY-HA1$|^SECONDARY$|^SECONDARY-HA1$|^PRIMARY-SCALE1$|^PRIMARY-SCALE2$|^AUXVM-SCALE1$|^AUXVM-SCALE2$

请让我知道如何才能得到这个。

1 个答案:

答案 0 :(得分:0)

sysLst = str(raw_input('Enter pipe separated list of items [PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|SCALE|ALL]: ')).upper().strip()
if "ALL" in sysLst:
    nsysLst1 = "PRIMARY|PRIMARY-HA1|SECONDARY|SECONDARY-HA1|^SCALE$"
    nsysLst = nsysLst1.replace("^SCALE$", "PRIMARY-SCALE1|PRIMARY-SCALE2|AUXVM-SCALE1|AUXVM-SCALE2")
    output="|".join(["^" + sysList_t + "$" for sysList_t in nsysLst.split('|')])
else:    
    nsysLst1="|".join(["^" + sysList_t + "$" for sysList_t in sysLst.split('|')])
    output = nsysLst1.replace("^SCALE$", "PRIMARY-SCALE1|PRIMARY-SCALE2|AUXVM-SCALE1|AUXVM-SCALE2")
print output