<bdo id='ZdGQL'></bdo><ul id='ZdGQL'></ul>

<legend id='ZdGQL'><style id='ZdGQL'><dir id='ZdGQL'><q id='ZdGQL'></q></dir></style></legend>
<tfoot id='ZdGQL'></tfoot>
    1. <small id='ZdGQL'></small><noframes id='ZdGQL'>

      1. <i id='ZdGQL'><tr id='ZdGQL'><dt id='ZdGQL'><q id='ZdGQL'><span id='ZdGQL'><b id='ZdGQL'><form id='ZdGQL'><ins id='ZdGQL'></ins><ul id='ZdGQL'></ul><sub id='ZdGQL'></sub></form><legend id='ZdGQL'></legend><bdo id='ZdGQL'><pre id='ZdGQL'><center id='ZdGQL'></center></pre></bdo></b><th id='ZdGQL'></th></span></q></dt></tr></i><div id='ZdGQL'><tfoot id='ZdGQL'></tfoot><dl id='ZdGQL'><fieldset id='ZdGQL'></fieldset></dl></div>

        为什么 Python 'for word in words:' 迭代单个字符而不是单词?

        Why does Python #39;for word in words:#39; iterate on individual characters instead of words?(为什么 Python for word in words: 迭代单个字符而不是单词?)
          <tbody id='aXPSL'></tbody>

            <small id='aXPSL'></small><noframes id='aXPSL'>

            <i id='aXPSL'><tr id='aXPSL'><dt id='aXPSL'><q id='aXPSL'><span id='aXPSL'><b id='aXPSL'><form id='aXPSL'><ins id='aXPSL'></ins><ul id='aXPSL'></ul><sub id='aXPSL'></sub></form><legend id='aXPSL'></legend><bdo id='aXPSL'><pre id='aXPSL'><center id='aXPSL'></center></pre></bdo></b><th id='aXPSL'></th></span></q></dt></tr></i><div id='aXPSL'><tfoot id='aXPSL'></tfoot><dl id='aXPSL'><fieldset id='aXPSL'></fieldset></dl></div>
            <legend id='aXPSL'><style id='aXPSL'><dir id='aXPSL'><q id='aXPSL'></q></dir></style></legend>
                <tfoot id='aXPSL'></tfoot>
                  <bdo id='aXPSL'></bdo><ul id='aXPSL'></ul>

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

                  问题描述

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

                  When I run the following code on a string words:

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

                  我得到的是字母而不是单词的输出字典理解:

                  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 'for word in words:' 迭代单个字符而不是单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                  • <bdo id='rFXX2'></bdo><ul id='rFXX2'></ul>

                      <tbody id='rFXX2'></tbody>
                  • <small id='rFXX2'></small><noframes id='rFXX2'>

                    1. <i id='rFXX2'><tr id='rFXX2'><dt id='rFXX2'><q id='rFXX2'><span id='rFXX2'><b id='rFXX2'><form id='rFXX2'><ins id='rFXX2'></ins><ul id='rFXX2'></ul><sub id='rFXX2'></sub></form><legend id='rFXX2'></legend><bdo id='rFXX2'><pre id='rFXX2'><center id='rFXX2'></center></pre></bdo></b><th id='rFXX2'></th></span></q></dt></tr></i><div id='rFXX2'><tfoot id='rFXX2'></tfoot><dl id='rFXX2'><fieldset id='rFXX2'></fieldset></dl></div>
                      <tfoot id='rFXX2'></tfoot>

                            <legend id='rFXX2'><style id='rFXX2'><dir id='rFXX2'><q id='rFXX2'></q></dir></style></legend>