简体   繁体   中英

Why is my Python program not finding the difference between the two sets?

I'm able to successfully get all the IP addresses on my wifi and I'm storing them in a knownIP list. Then I'm testing it by accessing the WiFi network with my iPhone. The second scan successfully detects and adds the new IP to a list. I make both of the lists a set and then use the difference. But it's only printing set() and it's an empty set.

# print statements are added for troubleshooting purposes
knownIPlist = initial_scan()
print(knownIPlist)
knownIPList_set = set(knownIPlist)
scanWifi = True
while scanWifi:
    print("Turn on iPhone") 
    time.sleep(15)
    print("Scanning...")
    scanIPlist = scan()
    print(scanIPlist)
    scanIPlist_set = set(scanIPlist)
    newIPlist = knownIPList_set.difference(scanIPlist_set)
    print(newIPlist)
    # for newIP in newIPlist:
    #    print(newIP)
    print("waiting...")

The output is:

'192.168.1.64', '192.168.1.77', '192.168.1.190', '192.168.1.208', '192.168.1.216', '192.168.1.217', '192.168.1.220', '192.168.1.222', '192.168.1.227', '192.168.1.254', '192.168.1.66'

Turn on iPhone

Scanning...
'192.168.1.64', '192.168.1.77', '192.168.1.190', '192.168.1.208', '192.168.1.210', '192.168.1.216', '192.168.1.217', '192.168.1.220', '192.168.1.222', '192.168.1.227', '192.168.1.254', '192.168.1.66'

set()

waiting...

It successfully finds the new IP address (.210) in the new scan but it just prints out "set ()". It's just an empty set. Why is it not adding and printing the new.210 IP address to the newIPlist?

set.difference returns the elements in the left hand set that aren't in the right hand set . Adding elements to the right hand set can only reduce the number of elements in the result, not add to it. As written, you'll report IPs that have disappeared since the original set was created, not new IPs that have appeared since then.

To report only new IPs, reverse the call:

newIPlist = scanIPlist_set.difference(knownIPList_set)

Or for brevity (and to make it clearer that you're taking away the elements from one, where the named difference method is less clear, and likely the source of your confusion):

newIPlist = scanIPlist_set - knownIPList_set

knownIPList_set.difference(scanIPlist_set) returns all the elements of knownIPList_set that aren't in scanIPlist_set . The .210 IP is in scanIPlist_set , but not knownIPList_set . So you need to reverse the operation:

newIPlist = scanIPlist_set.difference(knownIPList_set)

Both the set contains same elements, so when you take the difference, it will result in the empty set and hence it is printing set().

Try printing,

if(knownIPList_set == scanIPlist_set):
    print("True")

@Brandon

You're trying the reverse that is why you are getting an empty set. To figure out, please run the below snippet:

knownIPList_set = {'192.168.1.64', '192.168.1.77', '192.168.1.190', '192.168.1.208', '192.168.1.216', '192.168.1.217', '192.168.1.220', '192.168.1.222', '192.168.1.227', '192.168.1.254', '192.168.1.66'}
scanIPlist_set = {'192.168.1.64', '192.168.1.77', '192.168.1.190', '192.168.1.208', '192.168.1.210', '192.168.1.216', '192.168.1.217', '192.168.1.220', '192.168.1.222', '192.168.1.227', '192.168.1.254', '192.168.1.66'}

newIPlist_incorrect = knownIPList_set.difference(scanIPlist_set)
print(newIPlist_incorrect)


newIPlist_correct = scanIPlist_set.difference(knownIPList_set)
print(newIPlist_correct)

The output will be as following:

set()
{'192.168.1.210'}

I hope it gives an idea of what is not right in your code. You should be subtracting knownIPList_set from scanIPlist_set. That's it.

Thank you so much for all your recommendations.

The problem was in the code prior to my questions. The knownIPlist and scanIPlist were type strings, so when I declared them as sets it was giving this output:

{'4', '2', '9', "'", '0', '5', '1', '.', '8', '6', '7', ',', ' '}

instead of the IPs, so comparing them wasn't doing anything. Also, this was the correct order to get the output of my iPhone joining my wifi.

newIPlist = scanIPlist_set.difference(knownIPList_set)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM