在…中有…的字典。。。在里面?

2024-09-24 02:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在查看其他人的代码以使其适合我自己的here,我在第44行中遇到了这个问题:

scores = {word: tfidf(word, blob, bloblist) for word in blob.words}

在Python2.7.6中尝试此操作时,在单词for后面的空格中出现语法错误。为什么是无效语法,代码中到底发生了什么?你知道吗


Tags: 代码inforhere语法单词blobword
1条回答
网友
1楼 · 发布于 2024-09-24 02:28:29

词典理解是backported only to 2.7 not to 2.6, from 3.1

Dictionary and set comprehensions ({ i: i*2 for i in range(3)}).

因此,在Python2.6中的等价物是

dict((word, tfidf(word, blob, bloblist)) for word in blob.words)

但是通过名单会稍微快一点

dict([(word, tfidf(word, blob, bloblist)) for word in blob.words])

相关问题 更多 >