使用if语句在列表中进行for循环,仅if语句在列表的最后位置运行

时间:2019-02-25 04:58:31

标签: python python-3.x

这是我的列表,因此models [0], names[0], mac_addresses[0]都需要计算我需要的mac地址

models = ["MR18","MR32", "MR18"]
names = ["David", "Bob", "Frank"]
mac_addresses = ["00:18:0A:2C:3D:5F", "00:18:0A:2d:3c:5F", "00:18:0A:2A:3B:5F"]

这些功能应根据if语句为True来运行。

def calc18(mac_address, name, mr):
    #Mac Address Breakdown
    print(name)
    mac_calc = mac_address[:2]
    mac_extractor_front = mac_address[2:6]
    mac_extractor_back = mac_address[8:]

    flag = True

    First_Pos_Hex = first_hex_calc(mac_calc, mr, flag)

    #Initial Mac Addresses
    list_2_4.append(mac_address)
    list_5.append(First_Pos_Hex + mac_extractor_front + mr_18_5ghz + mac_extractor_back)

    flag = False
    #First Time Calculation hex updated
    hex_updater = first_hex_calc(mac_calc, mr, flag)

    list_2_4.append(hex_updater + mac_extractor_front + mr_18_24ghz + mac_extractor_back)
    list_5.append(hex_updater + mac_extractor_front + mr_18_5ghz + mac_extractor_back)

    #Update self, after appending mac addresses    
    for i in range(15):
        counter = i + 1
        hex_updater = hex_calc(hex_updater, mr) 
        list_2_4.append(hex_updater + mac_extractor_front + mr_18_24ghz + mac_extractor_back)
        list_5.append(hex_updater + mac_extractor_front + mr_18_5ghz + mac_extractor_back)
        print(str(counter) + ") 2.4ghz: " + list_2_4[i] + "\t" + str(counter) + ") 5 Ghz: " + list_5[i] )

        for i in range(len(list_2_4)):
            writer(name, mac_address, list_2_4[i], list_5[i], i)


def calc32(mac_address, name):
    #Mac Address Breakdown
    mac_calc = mac_address[15:17]
    mac_extractor_front = mac_address[:6]
    mac_extractor_back = mac_address[8:15]


    #Initial Mac Addresses
    list_2_4.append(mac_extractor_front + mr_32_24ghz + mac_extractor_back + mac_calc)
    list_5.append(mac_extractor_front + mr_32_5ghz + mac_extractor_back + mac_calc)


    #Update self, after appending mac addresses    
    for i in range(15):
        counter = i + 1
        mac_calc = hex_calc(mac_calc, mr) 
        list_2_4.append(mac_extractor_front + mr_32_24ghz + mac_extractor_back + mac_calc)
        list_5.append(mac_extractor_front + mr_32_5ghz + mac_extractor_back + mac_calc)
        print(str(counter) + ") 2.4ghz: " + list_2_4[i] + "\t" + str(counter) + ") 5 Ghz: " + list_5[i] )
        writer(name, mac_address, list_2_4[i], list_5[i], i)

现在,我有了这个for循环,应该遍历models中的每个位置,然后再检查if语句并执行特定功能

因此,第一个迭代models[0]中存储了"MR18",然后将检查if语句并执行calc18()calc18()函数完成执行后,将返回for循环并运行下一个迭代,该迭代将为models[1],该迭代具有“ MR32”,然后再次检查if语句并执行calc32() 。然后,当calc32()完成执行后,移至models[2]

但是在我运行代码的情况下,代表“ MR18”的models[2]贯穿if语句,完全忽略了models[0]models[1]

for num, mod in enumerate(models):
    print(mod)
    if mod == "MR18":
        print("I have entered")
        calc18(mac_addresses[num], names[num], mod)
    if mod == "MR32":
        print("I have entered 32")
        calc32(mac_addresses[num], names[num])

如果这仍然令人困惑,请告诉我,我不确定是否允许使用图片,如果可以,我可以画一个视觉示例:(

1 个答案:

答案 0 :(得分:1)

由于您的代码无法在我的系统上运行,因此我运行一个更简单的代码版本,以说明您的代码确实应该遍历模型的每个元素。

models = ["MR18","MR32", "MR18"]
names = ["David", "Bob", "Frank"]
mac_addresses = ["00:18:0A:2C:3D:5F", "00:18:0A:2d:3c:5F", "00:18:0A:2A:3B:5F"]

def calc18(mac_address, name, mr):
    print("calc18 running")
    print(mac_address)
    print(name)
    print(mr)
def calc32(mac_address, name):
    print("calc32 running")
    print(mac_address)
    print(name)

for num, mod in enumerate(models):
    print(num)
    print(mod)
    if mod == "MR18":
        print("I have entered")
        calc18(mac_addresses[num], names[num], mod)
    if mod == "MR32":
        print("I have entered 32")
        calc32(mac_addresses[num], names[num])

产生:

0
MR18
I have entered
calc18 running
00:18:0A:2C:3D:5F
David
MR18
1
MR32
I have entered 32
calc32 running
00:18:0A:2d:3c:5F
Bob
2
MR18
I have entered
calc18 running
00:18:0A:2A:3B:5F
Frank
MR18

代码确实遍历列表的每个元素。问题不应该与for循环有关。

我在您的代码中看到writer,您可能正在其中写入文件。请检查是否将其设置为覆盖模式或追加模式。