擅长:python、mysql、java
<p>是你要找的吗?你知道吗</p>
<pre><code>happy_set = set([":)",":-)","=)",":D",":-D","=D"])
sad_set = set([":(",":-(","=("])
happy_maybe_sad = [tweet.split() for tweet in data for face in happy_set if face in tweet]
sad_maybe_happy = [tweet.split() for tweet in data for face in sad_set if face in tweet]
happy = [item for item in happy_maybe_sad if not in sad_maybe_happy]
sad = [item for item in sad_maybe_happy if not in happy_maybe_sad]
</code></pre>
<p>对于<code>happy...</code>和<code>sad...</code>,我坚持使用列表解决方案,因为项目的顺序可能是相关的。如果不是,那么使用<a href="https://docs.python.org/2/library/sets.html" rel="nofollow">^{<cd3>}</a>来实现性能可能更好。是加法,集合已经提供了<a href="https://docs.python.org/2/library/sets.html#set-objects" rel="nofollow">basic sets operations</a>(并集、交集等)</p>