2024-09-30 02:31:07 发布
网友
在下面的代码中
[{word: score_tweet(tweet) for word in tweet} for tweet in tweets]
我得到了一份口述清单:
我只想得到一个扁平的dict,比如:
{u'soad': 0.0, u'<3': 0.0, u'outros': 0.0, u'acredita': 0.0}
我应该如何更改代码? 注意:我使用的是python2.7。在
将for循环移到dict理解中:
for
{word: score_tweet(tweet) for tweet in tweets for word in tweet}
请记住,一行中的两个for循环很难读取。我会做这样的事情:
你需要一个中间步骤。在
words = [] tweets = ["one two", "three four"] for tweet in tweets: words.extend(tweet.split()) scores = {word: score_tweet(word) for word in words}
将
for
循环移到dict理解中:请记住,一行中的两个
^{pr2}$for
循环很难读取。我会做这样的事情:你需要一个中间步骤。在
相关问题 更多 >
编程相关推荐