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

2024-09-29 23:28:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经试了好几个循环,但似乎不能达到我想要的。以下是我所拥有的和我所缺少的:

#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个nic的主机的一部分。你知道吗

它将继续为下一台mac做同样的事情,直到没有剩下一台。这个清单应该很小,所以我不太担心效率。你知道吗

任何帮助都会很棒。你知道吗


Tags: andoftoinfordataifis

热门问题