Set weird behavior (python)

Zion
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.

Rahul Gupta

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 in t

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python datetime weird behavior

From Dev

python thread weird behavior

From Dev

python dictionary weird behavior

From Dev

Weird scoping behavior in python

From Dev

Weird behavior in Python in array initialization

From Dev

Python, weird memory consumption behavior

From Dev

Weird behavior in python list concatenation

From Dev

The weird behavior of the print function in python

From Dev

Weird python file path behavior

From Dev

Weird sets behavior in python REPL

From Dev

Weird behavior with a lot of numbers python

From Dev

Weird behavior of Directive Scope when set to true

From Dev

Weird bash behavior when IFS is set for a for loop

From Dev

Excel weird behavior when workbook set to not visible

From Dev

python dictionary weird behavior with dict as container

From Dev

Weird Behavior When Slicing a List in Python

From Dev

Python Flask weird logging behavior (kubernetes)

From Dev

Python: Weird behavior while using `yield from`

From Dev

Knapsack algorithm, weird behavior (python3)

From Dev

Python getting ranks of elements in list weird behavior

From Dev

Weird behavior of barplot from python matplotlib with datetime

From Dev

multiprocessing.Queue weird python behavior

From Dev

Decimal Python Library has weird behavior

From Dev

Weird behavior of (^)

From Dev

Puzzled by this Python set behavior

From Dev

Weird behavior in SharePoint library set up to require content approval

From Java

Weird behavior of java.util.Set.contains(Object o)

From Dev

Weird behavior of using set.seed multiple times

From Dev

Weird behavior of gsettings set a new desktop.background with cron