如何解决格式问题,使字符串不会在for循环中重复?

时间:2019-05-08 20:16:37

标签: python

我的问题是,当我运行代码时,我的格式为每个输出重复相同的名称两次,如下所示:

MW SCHEDULE OF ['RAM PAR 3500', 'RAM PAR 4500']  is 136.0 MW 
MW SCHEDULE OF ['RAM PAR 3500', 'RAM PAR 4500']  is 136.0 MW
FROM_BUS=[126,126]
TO_BUS=[126,126]
CKT=[1,2]
NAME=['RAM 1', 'RAM 2']
for FROM_BUS,TO_BUS,CKT in zip(FROM_BUS, TO_BUS, CKT):
    [ierr, cmpval] = psspy.brnflo(FROM_BUS,TO_BUS,str(CKT))
    cmpvali=abs(cmpval.real)
    output10='MW SCHEDULE OF {}  is {} MW '.format(NAME,(round(cmpvali,0)))
    print(output10)

相反,我希望我的结果像这样:

MW SCHEDULE OF ['RAM PAR 3500']  is 136.0 MW
MW SCHEDULE OF ['RAM PAR 4500']  is 136.0 MW

2 个答案:

答案 0 :(得分:0)

您在NAME函数中缺少zip,请尝试以下操作:
(还整理了一下代码并修复了变量命名问题)

FROM_BUS = [126, 126]
TO_BUS = [126, 126]
CKT = [1, 2]
NAME = ['RAM 1', 'RAM 2']
for from_bus, to_bus, ckt, name in zip(FROM_BUS, TO_BUS, CKT, NAME):
    [ierr, cmpval] = psspy.brnflo(from_bus, to_bus, str(ckt))
    cmpvali = abs(cmpval.real)
    output10 = 'MW SCHEDULE OF {}  is {} MW '.format(name, (round(cmpvali, 0)))
    print(output10)

答案 1 :(得分:0)

NAMEcmpvali有什么关系?您无需遍历NAME中的值,因此当然会同时打印两者。

我不知道您要完成什么,但是也许以下是您想要的?

FROM_BUS=[126,126]
TO_BUS=[126,126]
CKT=[1,2]
NAME=['RAM 1', 'RAM 2']
# ADD NAME TO zip()
for FROM_BUS,TO_BUS,CKT,NAME in zip(FROM_BUS, TO_BUS, CKT, NAME):
    [ierr, cmpval] = psspy.brnflo(FROM_BUS,TO_BUS,str(CKT))
    cmpvali=abs(cmpval.real)
    output10='MW SCHEDULE OF {}  is {} MW '.format(NAME,(round(cmpvali,0)))
    print(output10)