Python循环检查列表中的每个项目与其他元素

时间:2017-10-11 21:46:39

标签: python python-2.7 list loops

我一直在尝试几个循环,但似乎无法达到我想要的效果。这是我拥有的和我所缺少的:

#Gets a list of all MAC addresses 
MACList = fetchMAC.MAC
i = 0
while i<len(MACList):
    #Gets data associated to MAC
    hostLogin(user, password, MACList[i])
    #After above function is executed, below variables will change values for each MAC, it adds second MAC to SecondMAC
    # It can not see ip associated to SecondMac so it puts "NA" under IPAddr2 as placeholder
    print "Data: ", MACList[i], SecondMAC, IPAddr1,IPAddr2, Host, Loc
    #Add all this data in a database
    MacDB.updateDB(MACList[i], SecondMAC, IPAddr,IPAddr2, Host, Loc)
    i +=1
    # I am stuck here
    # so after adding very first MAC, the loop starts over with i = 1
    # It goes thru hostlogin() function as usual and gets all data for second MAC. 
    # But before adding its own row in DB by calling updateDB(), I would like to check if this MAC is already exists. Three ways I believe we can check:
    # 1 - Compare new MAC is in SecondMAC variable of PREVIOUS MAC loop
    # 2 - Opposite of 1.  Check if previous MAC is in SecondMAC variable of THIS loop.
    # 3 - Query DB and see if it already exists under SecondMAC column of BD.
    # if second MAC in MACList is already in DB as SecondMAC
    # then run it though below func and if not, add it normally and move on to next MAC

    MacDB.editDB(MACList[i], SecondMAC, IPAddr)

    # Above func edits the row where MAC was stored and Sets the value of IPAddr2 for that SecondMAC.

不幸的是,当我使用hostLogin函数获取数据时,它可以看到MAC地址,但只能看到与该主机关联的IP地址,而不能看到与同一主机上的第二个MAC关联的IP。这是我必须进行比较并确保我没有添加MAC / IP的新条目的主要原因,该条目是具有2个NICS的主机的一部分。

它将继续为下一个MAC做同样的事情,直到没有剩下。这份清单应该相当小,所以我对效率并不太担心。

任何帮助都会很棒。

0 个答案:

没有答案