我有一个tweet列表,它被分组为列表中的tweet块,如下所示:
[[tweet1, tweet2, tweet3],[tweet4,tweet5,tweet6],[tweet7, tweet8, tweet9]]我想统计每个单词在每个子组中出现的次数。要做到这一点,我需要将每个tweet分成单独的单词。我想使用类似于str.split(‘')的东西,但收到一个错误:
AttributeError: 'list' object has no attribute 'split' 有没有办法把每条推文分成各自的单词?结果应该类似于:
[['word1', 'word2', 'word3', 'word2', 'word2'],['word1', 'word1', 'word3', 'word4', 'word5'],['word1', 'word3', 'word3', 'word5', 'word6']]发布于 2015-04-22 09:28:16
如果您有一个字符串列表
tweets = ['a tweet', 'another tweet']然后,您可以使用列表理解拆分每个元素
split_tweets = [tweet.split(' ')
for tweet in tweets]因为它是一个tweet列表:
tweet_groups = [['tweet 1', 'tweet 1b'], ['tweet 2', 'tweet 2b']]
tweet_group_words = [[word
for tweet in group
for word in tweet.split(' ')]
for group in tweet_groups]它将给出单词列表的列表。
如果你想计算不同的单词,
words = [set(word
for tweet in group
for word in tweet.split(' '))
for group in tweet_groups]发布于 2015-04-22 09:27:55
你想要这样的东西:
l1 = [['a b', 'c d', 'e f'], ['a b', 'c d', 'e f'], ['a b', 'c d', 'e f']]
l2 = []
for i,j in enumerate(l1):
l2.append([])
for k in j:
l2[i].extend(k.split())
print(l2)DEMO
发布于 2015-04-22 09:29:11
groups = [["foo bar", "bar baz"], ["foo foo"]]
[sum((tweet.split(' ') for tweet in group), []) for group in groups]
# => [['foo', 'bar', 'bar', 'baz'], ['foo', 'foo']]编辑:似乎需要一个解释。
每组[... for group in groups]的
- For each tweet, split into words `(tweet.split(' ') for tweet in group)`
- Concatenate the split tweets `sum(..., [])`
https://stackoverflow.com/questions/29786086
复制相似问题