def _get_apps(path):
"""gets only all the apps"""
return {app for app in os.listdir(path) if ".py" not in app}
apps = _get_apps(r"C:\Users\Hello\Desktop\Test")
css_apps = _get_apps(r"C:\Users\Hello\Desktop\Test2")
print(apps.difference(css_apps))
I'm trying to get the difference between two folders in my desktop. using the code above
individually the output is right it returns a set as expected
individual print:
print(apps)
print(css_apps)
Output:
{Music}
{Music,Css}
however doing:
print(apps.difference(css_apps))
Output:
set()
whats going on?
it returned a set as expected but somehow I can't do set operations on the returned set.
This is because difference
operation calculates the elements which are in apps
set but not in css_apps
set. Now there are no elements satisfying this criteria so you get an empty set.
s.difference(t)
creates a :
new set with elements in
s
but not int
Perhaps, what you need is .symmetric_difference()
. This will create a new set with elements in either of the sets but not both.
In [1]: s1 = set([1]) # First set
In [2]: s2 = set([1,2]) # Second set
In [3]: s1.difference(s2) # Creates a new set with elements in s1 but not in s2
Out[3]: set() # empty because no elements satisfy this criteria
In [4]: s2.difference(s1) # Creates a new set with elements in s2 but not in s1
Out[4]: {2} # element '2' exists in s2 but not in s1
In [5]: s1.symmetric_difference(s2) # Returns new set with elements in either s1 or s2 but not both
Out[5]: {2}
In [6]: s2.symmetric_difference(s1)
Out[6]: {2}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments