我想獲得2個容器之間的差異,但容器是在一個奇怪的結構,所以我不知道什麼是最好的方式來執行它的差異。一個容器類型和結構我不能改變,但我可以改變其他的(可變分界)。在元組列表上執行設置的操作差異
delims = ['on','with','to','and','in','the','from','or']
words = collections.Counter(s.split()).most_common()
# words results in [("the",2), ("a",9), ("diplomacy", 1)]
#I want to perform a 'difference' operation on words to remove all the delims words
descriptive_words = set(words) - set(delims)
# because of the unqiue structure of words(list of tuples) its hard to perform a difference
# on it. What would be the best way to perform a difference? Maybe...
delims = [('on',0),('with',0),('to',0),('and',0),('in',0),('the',0),('from',0),('or',0)]
words = collections.Counter(s.split()).most_common()
descriptive_words = set(words) - set(delims)
# Or maybe
words = collections.Counter(s.split()).most_common()
n_words = []
for w in words:
n_words.append(w[0])
delims = ['on','with','to','and','in','the','from','or']
descriptive_words = set(n_words) - set(delims)
看起來有效我想我會用它,但單詞是元組列表我怎麼能說「單詞[delim]」? – 2012-03-29 09:45:53
@JakeM - 將其直接應用於Counter對象。 – eumiro 2012-03-29 09:48:38
啊,我在想詞是Counter對象 – 2012-03-29 09:49:03