设置奇怪的行为(python)
我正试图找出桌面上两个文件夹之间的区别。使用上面的代码 单独输出是正确的,它会按预期返回一个集合 单独打印:设置奇怪的行为(python),python,Python,我正试图找出桌面上两个文件夹之间的区别。使用上面的代码 单独输出是正确的,它会按预期返回一个集合 单独打印: 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:
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))
print(apps)
print(css_apps)
输出:
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))
print(apps)
print(css_apps)
但是:
{Music}
{Music,Css}
输出:
print(apps.difference(css_apps))
发生什么事了
它按预期返回了一个集合,但不知何故,我无法对返回的集合执行集合操作。这是因为
差异
操作计算应用程序
集合中但不在css\u应用程序
集合中的元素。现在没有满足此条件的元素,因此得到一个空集
创建一个:
新集合,元素位于s
但不在t
也许,你需要的是。这将创建一个新的集合,其中包含任意一个集合中的元素,但不能同时包含这两个集合中的元素
set()
可能建议将
symmetric_difference
作为difference
的替代方案。(对称差异是否有用取决于对结果的处理。)我认为顺序对差异并不重要。我的bad@WarrenWeckesser那正是我要找的。现在我明白了什么是对称差。两个集合之间的差异,无论集合的顺序如何compared@WarrenWeckesser谢谢你的建议,更新了ans。