Best way to find the intersection of multiple sets?
-
23-09-2019 - |
문제
I have a list of sets:
setlist = [s1,s2,s3...]
I want s1 ∩ s2 ∩ s3 ...
I can write a function to do it by performing a series of pairwise s1.intersection(s2)
, etc.
Is there a recommended, better, or built-in way?
해결책
사이트 모음 관리자가 사용할 때 앱이 작동하면 필요한 권한이 있으며 더 많은 권한을 추가하지 않아도됩니다.
일반 사용자를 위해 작동하기 위해 필요한 것은 allowAppOnlyPolicy를 추가하여 시작한 것입니다.
그러나 allowAppOnlyPolicy를 추가하기 만하면 앱 만 사용 권한 만 실행할 수있는 앱 권한을 부여 할 것입니다.누락 된 두 번째 단계는 앱이 런타임에 필요한 경우 사용자 권한에 관계없이 일부 작업을 수행하려는 경우 을 지정합니다.
앱 만 사용할 수있는 액세스 토큰을 가져오고 TokenHelper.GetAppOnlyAccessToken
에 전달 된이 액세스 토큰을 사용하여 최종적으로 클라이언트 컨텍스트를 생성하기 위해 TokenHelper.GetClientContextWithAccessToken
를 호출 하여이 작업을 수행합니다.
다른 팁
As of 2.6, set.intersection
takes arbitrarily many iterables.
>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s3 = set([2, 4, 6])
>>> s1 & s2 & s3
set([2])
>>> s1.intersection(s2, s3)
set([2])
>>> sets = [s1, s2, s3]
>>> set.intersection(*sets)
set([2])
불가능합니다.이를 수행하는 유일한 방법은 다음과 같습니다.
- ClientContext (String) - 새 인스턴스를 초기화합니다. 지정된 SharePoint 사이트의 ClientContext 클래스 절대 URL.
- ClientContext (URI) - ClientContext의 새 인스턴스를 초기화합니다.
지정된 URI 객체가있는 사이트의 클래스.
그러나 사이트를 사용할 수 있습니다..openwebbyid () guid 에 의해 웹 가져 오기
If you don't have Python 2.6 or higher, the alternative is to write an explicit for loop:
def set_list_intersection(set_list):
if not set_list:
return set()
result = set_list[0]
for s in set_list[1:]:
result &= s
return result
set_list = [set([1, 2]), set([1, 3]), set([1, 4])]
print set_list_intersection(set_list)
# Output: set([1])
You can also use reduce
:
set_list = [set([1, 2]), set([1, 3]), set([1, 4])]
print reduce(lambda s1, s2: s1 & s2, set_list)
# Output: set([1])
However, many Python programmers dislike it, including Guido himself:
About 12 years ago, Python aquired lambda, reduce(), filter() and map(), courtesy of (I believe) a Lisp hacker who missed them and submitted working patches. But, despite of the PR value, I think these features should be cut from Python 3000.
So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.
Here I'm offering a generic function for multiple set intersection trying to take advantage of the best method available:
def multiple_set_intersection(*sets):
"""Return multiple set intersection."""
try:
return set.intersection(*sets)
except TypeError: # this is Python < 2.6 or no arguments
pass
try: a_set= sets[0]
except IndexError: # no arguments
return set() # return empty set
return reduce(a_set.intersection, sets[1:])
Guido might dislike reduce
, but I'm kind of fond of it :)