python - finding items that only belong to one set in a list of sets -
this question has answer here:
- subtraction on list of sets 4 answers
question: have list of sets, how find items unique each set?
=====
i playing around nltk , curious words unique each corpus, made quick script check symmetric difference of word sets between each corpus. didn't work (see image) reasons obvious in retrospect, i'm still stuck how find unique items in each set?
i surprised not find on so thought i'd post it
![[1]](https://i.stack.imgur.com/ca332.png)
my code:
import pandas pd import re nltk.corpus import words, brown, webtext, reuters, wordnet, gutenberg words_only_in_one_corpus = set() corpus in {words, brown, webtext, reuters, wordnet, gutenberg}: words_only_in_one_corpus ^= {word.lower() word in set(corpus.words())} df = pd.dataframe(data=list(words_only_in_one_corpus), columns=["words"]) corpus in {words, brown, webtext, reuters, wordnet, gutenberg}: corpus_name = re.findall(".*\\\\(.*?)'>", str(corpus))[-1] if corpus_name == "words": corpus_name = "words_corpus" print(corpus_name, "...") df[corpus_name] = df.words.isin( {word.lower() word in set(corpus.words())} ) df
let's arr list of sets.
newarr = [] theset in arr: tempset = () item in theset: check = 1 anotherset in arr: if item in anotherset , anotherset != theset: check *= 0 elif anotherset.count(item) > 1: check *= 0 if check == 1: tempset += item newarr.append(tempset)
Comments
Post a Comment