884. Uncommon Words from Two Sentences 用collections模組裡的Counter類物件計數,返回字典中的key
阿新 • • 發佈:2018-12-30
We are given two sentences A
and B
. (A sentence is a string of space separated words. Each wordconsists only of lowercase letters.)
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Return a list of all uncommon words.
You may return the list in any order.
Example 1:
Input: A = "this apple is sweet", B = "this apple is sour" Output: ["sweet","sour"]
Example 2:
Input: A = "apple apple", B = "banana" Output: ["banana"]
class Solution: def uncommonFromSentences(self, A, B): """ :type A: str :type B: str :rtype: List[str] """ c=collections.Counter((A + ' ' + B).split(' ')) for k,v in c.items(): if v == 1: return k
reference:
collections.Counter
集合的方法:
http://www.runoob.com/python3/python3-set.html
字典的方法:
http://www.runoob.com/python/python-dictionary.html