【问题标题】:What do they mean by "filter the characters, normalize to lowercase?"“过滤字符,标准化为小写”是什么意思?
【发布时间】:2016-02-04 10:08:29
【问题描述】:

我正在做“编程风格练习”中的第一个练习,但我很难理解这些说明。在从《傲慢与偏见》中读取输入文件时,其中一条指令说了一些没有任何意义的内容。当他们说“过滤字符,规范化为小写”时,说明是什么意思?你如何过滤字符?

【问题讨论】:

  • 听起来它希望您在对它们进行任何处理之前将它们全部更改为小写。
  • 查看How do I verify that a string only has letters, numbers, underscores, and dashes,然后将其修改为只允许字母数字字符(然后是小写字母)
  • 我还应该注意,实际练习是“过滤字符,识别单词,并将它们规范化为小写”,书中包含的示例代码使用.isalnum()

标签: python-2.7 python-3.x


【解决方案1】:

这样读,“确保每个字符都是小写的”。正如使用一个函数来确保每个字符都是小写的。

【讨论】:

    【解决方案2】:

    处理角色时,请执行以下操作:

    >>> 'It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.'.lower()
    

    变成:

    'it is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.'
    

    【讨论】:

      猜你喜欢
      • 2014-06-05
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 2011-04-01
      相关资源
      最近更新 更多