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

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                  • <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>