2

わかりましたので、次のように 2 つのリストがあり、1 つは単語です。

["happy", "sad", "angry", "jumpy"]

そして、次のようなフレーズのリスト:

["I'm so happy with myself lately!", "Johnny, im so sad, so very sad, call me", "i feel like crap. SO ANGRY!!!!"]

単語の最初のリストを使用して、フレーズのリスト内の単語の出現頻度を見つけたいと思います。スペースで区切られた実際の単語を引き出すか、それらの出現回数だけを引き出すかは気にしません。

私が調べたところ、 re モジュールとフィルターが適しているようです。

また、必要なものについての説明が不明な場合は、お知らせください。

4

3 に答える 3

4
>>> phrases = ["I'm so happy with myself lately!", "Johnny, im so sad, so very sad, call me", "i feel like crap. SO ANGRY!!!!"]
>>> words = ["happy", "sad", "angry", "jumpy"]
>>> 
>>> for phrase in phrases:
...     print phrase
...     print {word: phrase.count(word) for word in words}
... 
I'm so happy with myself lately!
{'jumpy': 0, 'angry': 0, 'sad': 0, 'happy': 1}
Johnny, im so sad, so very sad, call me
{'jumpy': 0, 'angry': 0, 'sad': 2, 'happy': 0}
i feel like crap. SO ANGRY!!!!
{'jumpy': 0, 'angry': 0, 'sad': 0, 'happy': 0}
于 2012-07-08T16:33:34.700 に答える
2

非常にシンプルで簡単なソリューション:

>>> phrases = ["I'm so happy with myself lately!", "Johnny, im so sad, so very sad, call me", "i feel like crap. SO ANGRY!!!!"]
>>> words = ["happy", "sad", "angry", "jumpy"]
>>> for phrase in phrases:
        for word in words:
            if word in phrase:
                print('"{0}" is in the phrase "{1}".'.format(word, phrase))

"happy" is in the phrase "I'm so happy with myself lately!".
"sad" is in the phrase "Johnny, im so sad, so very sad, call me".
于 2012-07-08T16:25:02.023 に答える
1
>>> phrases = ["I'm so happy with myself lately!", "Johnny, im so sad, so very sad, call me", "i feel like crap. SO ANGRY!!!!"]
>>> words = ["happy", "sad", "angry", "jumpy"]
>>> words_in_phrases = [re.findall(r"\b[\w']+\b", phrase.lower()) for phrase in phrases]
>>> words_in_phrases
[["i'm", 'so', 'happy', 'with', 'myself', 'lately'], ['johnny', 'im', 'so', 'sad', 'so', 'very', 'sad', 'call', 'me'], ['i', 'feel', 'like', 'crap', 'so', 'angry']]
>>> word_counts = [{word: phrase.count(word) for word in words} for phrase in words_in_phrases]
>>> word_counts
[{'jumpy': 0, 'angry': 0, 'sad': 0, 'happy': 1}, {'jumpy': 0, 'angry': 0, 'sad': 2, 'happy': 0}, {'jumpy': 0, 'angry': 1, 'sad': 0, 'happy': 0}]
>>> 

行については、word_counts = [{word: phrase.count(word) for word in words} for...Python 2.7+ が必要です。何らかの理由で Python 2.7 未満を使用している場合は、その行を次のように置き換えます。

>>> word_counts = [dict((word, phrase.count(word)) for word in words) for phrase in words_in_phrases]
于 2012-07-08T16:50:28.393 に答える