【问题标题】:Python appending errorPython附加错误
【发布时间】:2015-07-09 09:01:00
【问题描述】:
x = []
with open(filechoice) as fileobj:
    for word in fileobj:  
       for ch in word:

           f = ord(ch)
           x = x.append(ord(ch))

但它返回此错误:

"AttributeError: 'NoneType' object has no attribute 'append'"

我该如何解决这个错误?

【问题讨论】:

标签: python append


【解决方案1】:

list.append() 方法返回None,您替换存储在x 中的列表为该返回值:

x = x.append(ord(ch))

这里不要分配回xlist.append() 更改列表就地

with open(filechoice) as fileobj:
    for word in fileobj:  
       for ch in word:
           x.append(ord(ch))

您可以使用列表推导来构建列表:

with open(filechoice) as fileobj:
    x = [ord(ch) for word in fileobj for ch in word]

【讨论】:

  • 你不能让别人回答python问题几次吗:-|
  • 是的,我同意@DavidGreydanus,你必须忽略简单的问题。 ;)
  • @DavidGreydanus:有人created activity profiles参加选举;我不回答 Python 问题 when the circles are small通常。 :-)
猜你喜欢
  • 2015-05-31
  • 1970-01-01
  • 2021-05-13
  • 2023-03-19
  • 1970-01-01
  • 2015-07-16
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多