检测阵列中的更改并运行命令以获取新值

vke*_*nos 0 python arrays events

我有一个IP阵列不时变化,我希望每个新的IP运行命令.

我的代码是:

while (network.status!="connected"):
    p=network.connections  
    for i in p:
        print i.ip  #checks the IP's in the array i
    time.sleep(10)    
Run Code Online (Sandbox Code Playgroud)

所以我希望每当数组i中有一个新值来运行一个特定的命令.在python中执行此操作的最有效方法是什么.

Thi*_*ter 5

使用a set并查看每个循环中的差异:

old = set()
while network.status != "connected":
    p = set(network.connections)
    for i in p - old:
        print i.ip # new ips that were added
    for i in old - p:
        print i.ip # old ips that were removed
    old = p
    time.sleep(10)   
Run Code Online (Sandbox Code Playgroud)