在字符串中包含多个包含的嵌套IF无法正常工作

时间:2019-06-04 07:48:09

标签: python python-3.x

我想使用嵌套if在字符串中查找多个字符串,例如:string.contain.any(多个值)

要获取铃声名称列表,我要打印“区域名称”。

我尝试使用python 3.4

下面是我测试过的程序----

#A list containing Ring Names
ringlist=['Bronkhorstspruit Optic Access Ring 3', 'Bronkhorstspruit Optic/MW Access Ring 3', 'Bronkhorstspruit Optic/MW Access Ring 3', 'Bronkhorstspruit Optic/MW Access Ring 3', 'Rosebank Ring 4', 'Meyersdal Access Ring 10', 'Meyersdal Access Ring 1', 'Krugersdorp Access Ring 3', 'Krugersdorp Access Ring 2', 'Doornfontein Ring 9', 'Sunninghill Access Ring 2', 'Sandton Access Ring 2', 'Bronkhorstspruit Optic Access Ring 1', 'Sandton Access Ring 4', 'Doornfontein Ring 3', 'Sandton Access Ring 4', 'Potchefstroom Access Ring 1', 'PSI PAYG Ring 1', 'PRO-PPQ PAYG R1', 'PF Access Ring 15', 'Germiston Ring 13', 'PCE - PF PAYG R4', 'PSI Access Ring 3\xa0', 'Krugerdorp Access Ring 4', 'Krugerdorp Access Ring 4', 'Krugerdorp Access Ring 3', 'Power Park Access Ring 10', 'Doornfontein Ring 9']

#Function to find Area Name from Ring Name 
def find_area(ring):
        area=None
        if "Bronkhorstspruit" in ring:
            area="BK - Bronkhorstspruit"
        elif "PF Access" or "PAYG" or "PPQ-PF"  or "PRO-PPQ" in ring:
            area="Geo Redundant Rings"
        elif "Klerksdorp" in ring:
            area="KS - Klerksdorp"
        elif "Centurion" in ring:
            area="PCE - Centurion MSC"
        elif "PF Access" in ring:
            area="PF - Parksfield BSC"
        elif "Pilanesberg" in ring:
            area="PL - Pilanesberg"
        elif "Potchefstroom" or "Carletonville" in ring:
            area="PO - Potchefstroom"
        elif "PPQ Access" in ring:
            area="PPQ - Persequor Park BSC"
        elif "PRO Access" or "PRO East"  "PRO MW" "PTA North" in ring:
            area="PRO - Rosslyn MSC"
        elif "Silverton" in ring:
            area="PSI - Silverton MSC"
        elif "Rustenburg" in ring:
            area="RT - Rustenburg"
        elif "Germiston" in ring:
            area="JGE - Germiston"
        elif "Kempton" in ring:
            area="KM - Kempton Park"
        elif "Midrand" in ring:
            area="MTA - Midrand MSC"
        elif "RN Ring" in ring:
            area="RN - Randburg"
        elif "Rosebank" in ring:
            area="RS - Rosebank"
        elif "Sandton" in ring:
            area="SD - Sandton"
        elif "Sunninghill" in ring:
            area="SN - Sunninghill"
        elif "Doornfontein" in ring:
            area="JDO - Doornfontein BSC"
        elif "Florida" in ring:
            area="JFL - Florida MSC"
        elif "Meyersdal" in ring:
            area="JME - Meyersdal BSC"
        elif "Krugerdorp" in ring:
            area="KD - Krugerdorp"
        elif "Power Park" in ring:
            area="PW - Power Park"
        elif "Vereeniging" in ring:
            area="VN - Vereeniging MSC"
        else:
            area="NOT FOUND"
        return area


for i in ringlist:
    print(i,"----------------",find_area(i))

下面是该程序的输出,根据环名称给出错误的区域。

Bronkhorstspruit Optic Access Ring 3 ---------------- BK - Bronkhorstspruit
Bronkhorstspruit Optic/MW Access Ring 3 ---------------- BK - Bronkhorstspruit
Bronkhorstspruit Optic/MW Access Ring 3 ---------------- BK - Bronkhorstspruit
Bronkhorstspruit Optic/MW Access Ring 3 ---------------- BK - Bronkhorstspruit
Rosebank Ring 4 ---------------- Geo Redundant Rings
Meyersdal Access Ring 10 ---------------- Geo Redundant Rings
Meyersdal Access Ring 1 ---------------- Geo Redundant Rings
Krugersdorp Access Ring 3 ---------------- Geo Redundant Rings
Krugersdorp Access Ring 2 ---------------- Geo Redundant Rings
Doornfontein Ring 9 ---------------- Geo Redundant Rings
Sunninghill Access Ring 2 ---------------- Geo Redundant Rings
Sandton Access Ring 2 ---------------- Geo Redundant Rings
Bronkhorstspruit Optic Access Ring 1 ---------------- BK - Bronkhorstspruit
Sandton Access Ring 4 ---------------- Geo Redundant Rings
Doornfontein Ring 3 ---------------- Geo Redundant Rings
Sandton Access Ring 4 ---------------- Geo Redundant Rings
Potchefstroom Access Ring 1 ---------------- Geo Redundant Rings
PSI PAYG Ring 1 ---------------- Geo Redundant Rings
PRO-PPQ PAYG R1 ---------------- Geo Redundant Rings
PF Access Ring 15 ---------------- Geo Redundant Rings
Germiston Ring 13 ---------------- Geo Redundant Rings
PCE - PF PAYG R4 ---------------- Geo Redundant Rings
PSI Access Ring 3  ---------------- Geo Redundant Rings
Krugerdorp Access Ring 4 ---------------- Geo Redundant Rings
Krugerdorp Access Ring 4 ---------------- Geo Redundant Rings
Krugerdorp Access Ring 3 ---------------- Geo Redundant Rings
Power Park Access Ring 10 ---------------- Geo Redundant Rings
Doornfontein Ring 9 ---------------- Geo Redundant Rings

Process finished with exit code 0

0 个答案:

没有答案