【问题标题】:Python: argument of type 'NoneType' is not iterablePython:“NoneType”类型的参数不可迭代
【发布时间】:2016-04-06 15:21:27
【问题描述】:

我想建立一个单词列表。对于每行上的每个单词,检查该单词是否已经在列表中,如果没有,则将其附加到列表中。程序完成后,按字母顺序对生成的单词进行排序和打印。 但是当我向列表中添加一个字符串时,它显示“'NoneType' 类型的参数不可迭代”。担心什么?

    fh = ("But soft what light through yonder window breaks"
    "It is the east and Juliet is the sun"
    "Arise fair sun and kill the envious moon"
    "Who is already sick and pale with grief")
    lst = list()
    for line in fh:
        words = line.split()
        for word in line:
                if word not in lst:
                    lst = lst.append(word)
    lst.sort()
    print lst

【问题讨论】:

  • 1) 尝试打印fh;这不是你想的那样。 2) 为什么你做words = line.split() 然后对words 什么都不做? for word in line: 应该是 for word in words: 吗? 3) 你希望lst = lst.append(word) 做什么? 4)为了有效地做到这一点,你应该使用set

标签: python list sorting iterable nonetype


【解决方案1】:

当你将两个字符串文字并排放置时,你会得到一个字符串:

>>> 'hi' 'there'
'hithere'

它们之间的空格不会影响这一点。这意味着for line in fh: 正在迭代每个字符,而不是每一行,从而产生意想不到的结果。您有几个选项可以解决此问题。

使用三引号字符串和splitlines()

fh = ("""But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief""")
fh = fh.splitlines()

结果:

>>> fh[0]
'But soft what light through yonder window breaks'

或使用listtuple

fh = ("But soft what light through yonder window breaks",
"It is the east and Juliet is the sun",
"Arise fair sun and kill the envious moon",
"Who is already sick and pale with grief")

结果:

>>> fh[0]
'But soft what light through yonder window breaks'

然后您split() 并保存该行,但您忘记使用它。 for word in line: 应该是 for word in words: 以迭代新对象。或者,您可以保持该行不变,而是将其上方的行 (words = line.split()) 更改为 line = line.split()

那么你还有一个问题:append() 就地操作并返回None。这意味着您只需调用该方法,它就会按照它所说的去做,而无需重新分配引用。如果你这样做,lst 会很快指向None,而你不希望这样。改为这样做:

lst.append(word)

【讨论】:

  • 这出现在这个答案的末尾。
【解决方案2】:

好吧,fh 是一个字符串,python 不知道如何按照你的意愿将它按行划分。您应该尝试添加逗号(或三引号,即"""):

fh = ("But soft what light through yonder window breaks",
    "It is the east and Juliet is the sun",
    "Arise fair sun and kill the envious moon",
    "Who is already sick and pale with grief")

这将使您获得:

>>> fh
('But soft what light through yonder window breaks', 
'It is the east and Juliet is the sun',
'Arise fair sun and kill the envious moon',
'Who is already sick and pale with grief')

这样做之后,您可能会正确使用for line in fh,然后您可能想要使用for word in words 而不是for word in line,这在这里没有多大意义。这将允许您遍历文本中每一行的所有单词。

此外,.append() 返回None(它正在就地更改列表),因此在这种情况下将其分配给变量是没有用的。因此,要解决这个问题,您可能应该创建一个不同的列表并使用.append()

另外,一般来说lst = lst.append(word) 没有任何意义(即使它“有效”),你想用这个来完成什么?

【讨论】:

    【解决方案3】:

    我会尽力帮助你的。

    首先,问题的质量可能会更好。我认为您应该尝试为您的 sn-p 提供一些理由,这样任何回应的人都可以通过改进您的解决方案以及其背后的想法来帮助您(让您成为更好的编码员)。

    其次,这个问题已经通过网络上的很多很好的解释得到了解决。检查例如item frequency count in python 有很多巧妙的解决方案。

    第三,这可能是您所期望的部分,您的问题表明您需要提高对 Python 语法、功能和一般算法思维的理解。让我详细说明。

    Python 语法和功能

    许多其他答案都强调您使用fh 的方式不是很好。通常,您会在 Python 中编写这样的多行字符串:

    fh = '''this is the first row
    second row
    third row'''
    

    您可以通过更好地理解 Python 的语法和功能来获得其他值得注意的示例,例如列表理解,内置类型等。走过的是Python tutorial。深入了解它,它非常有用!

    算法思维

    通常,当我看到像您这样的代码时,我会尝试找出问题是否是对工具(在本例中为 Python)或解决方案本身缺乏理解/知识。在你的情况下,我认为两者兼而有之。

    当谈到解决方案时,许多有经验的编码人员所做的(或至少在他们职业生涯的开始时所做的)就是弄清楚他们打算如何解决问题。一个典型的经验丰富的编码人员会检查他们的工具箱,看看哪些是可行的,然后应用它。

    我会通过使用集合来解决这个问题,使用集合数据类型来解决这个问题。在大多数情况下,集合非常方便,并且在离散数学领域得到了很好的描述。查看 Rosen 的书Discrete Mathematics and Its Applications,它涵盖了这些内容以及您将来会发现有用的一大堆其他内容。 (您可能可以找到很多描述集合的在线资源,但那本书是离散数学领域的参考书。我建议您获取它。)

    因为集合非常方便,Python 自然也内置了对它的支持。再次,Python 教程来拯救,查看sets 部分。

    可能的解决方案

    那么,可能的解决方案是什么样的?我会做以下事情:

    1. 确保所有内容都在多行字符串中。
    2. 将字符串拆分成一个列表。
    3. 根据列表内容创建集合。
    4. 转换回列表以便对其进行排序。
    5. 排序。
    6. 打印。

    这些都不需要任何forifstatements,并且使用了该语言的大量内置功能。

    希望此建议对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多