为什么Python'单词用词:'迭代单个字符而不是单词? [英] Why does Python 'for word in words:' iterate on individual characters instead of words?

查看:176
本文介绍了为什么Python'单词用词:'迭代单个字符而不是单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在字符串上运行以下代码时

def word_feats(words):
    return dict([(word, True) for word in words])
print(word_feats("I love this sandwich."))

我用字母而不是单词得到输出dict-comprehension:

I get the output dict-comprehension in letters instead of words:

{'a': True, ' ': True, 'c': True, 'e': True, 'd': True, 'I': True, 'h': True, 'l': True, 'o': True, 'n': True, 'i': True, 's': True, 't': True, 'w': True, 'v': True, '.': True}

我做错了什么?

推荐答案

你需要在空格上明确拆分字符串:

You need to explicitly split the string on whitespace:

def word_feats(words):
    return dict([(word, True) for word in words.split()])

这使用 str.split() 不带参数,拆分任意宽度的空格(包括制表符和行分隔符)。
字符串是一系列单独的字符,否则,直接迭代实际上只会遍历每个字符。

This uses str.split() without arguments, splitting on arbitrary-width whitespace (including tabs and line separators). A string is a sequence of individual characters otherwise, and direct iteration will indeed just loop over each character.

然而,分裂为单词必须是您需要自己执行的显式操作,因为不同的用例对如何将字符串拆分为单独的部分有不同的需求。例如,标点符号是否计算在内?如果用括号或引用括起来的话,那些字母应该被分开吗?等等。

Splitting into words, however, has to be an explicit operation you need to perform yourself, because different use-cases will have different needs on how to split a string into separate parts. Does punctuation count, for example? What about parenthesis or quoting, should words grouped by those not be split, perhaps? Etc.

如果您要做的就是将所有值设置为 True ,那么它的效率会更高使用 dict.fromkeys() 代替:

If all you are doing is setting all values to True, it'll be much more efficient to use dict.fromkeys() instead:

def word_feats(words):
    return dict.fromkeys(words.split(), True)

演示:

>>> def word_feats(words):
...     return dict.fromkeys(words.split(), True)
... 
>>> print(word_feats("I love this sandwich."))
{'I': True, 'this': True, 'love': True, 'sandwich.': True}

这篇关于为什么Python'单词用词:'迭代单个字符而不是单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆