【问题标题】:defining lists with .append in python在 python 中使用 .append 定义列表
【发布时间】:2015-03-10 23:59:34
【问题描述】:
height, width, device = 99, 99, 99
num_pieces = 3

#create datastructure to hold piece dimensions*

pieces = [[height, width, device]]

for i in range(num_pieces - 1):
    pieces = pieces.append([0,0,0])
    print "initial list"
    print pieces

#get dimentions for the rest of the list starting at second element*

for i in pieces[1: ]:
    print "list in loop"
    print pieces
# get height of the ith piece
    print "please enter the height of piece %d" % (pieces.index(i)+1)
    height = float(raw_input(">>"))
# get width of the ith piece
    print "please enter the width of piece %d" % (pieces.index(i)+1)
    width = float(raw_input(">>"))
# get device of the ith piece
    print "please enter the hanging device of piece %d" % (pieces.index(i)+1)
    device = float(raw_input(">>"))

# test if element is empty
if i == [0,0,0]:
    i = [height, width, device]

#view completeled list of dimensions
print pieces

我认为我的 .append 有问题(我不应该使用 .extend 吗?)

这是我的错误。我知道[1: ] 也有问题,但首先我想弄清楚如何解决 .append 问题

File "l_long.py", line 36, in <module>
    for i in pieces[1: ]:
TypeError: 'NoneType' object has no attribute '__getitem__'

【问题讨论】:

  • 请修复代码中的缩进,然后我们可以专注于其他各种问题。此外,如果您向我们提供一些您希望此代码打印的典型输出,那就太好了。
  • 你为什么用这个??? pieces.index(i)+1可以直接使用pieces[0][i]

标签: python list loops append typeerror


【解决方案1】:

我不知道你想做什么,但你的代码有一些缺陷:

pieces = [[height, width, device]]   
# Here 'height', 'width', and 'device' must be defined, else you will get NameError.

num_pieces # Must defined. It may be the length of the list??

pieces = pieces.append([0,0,0]) # This is going to override your list

【讨论】:

    【解决方案2】:

    由于这一行,您会收到错误:

    pieces = pieces.append([0,0,0])
    

    它将pieces 的值设置为None,因为append() 列表方法有效地返回了该值。大多数就地更改关联容器对象的容器方法不返回任何内容(Python 使它们看起来好像返回了值 None)。

    在您的代码中,这意味着 pieces 在第一次迭代后将其值更改为:

    for i in range(num_pieces - 1)
    

    ...因此出现了您看到的TypeError

    将行改为简单:

    pieces.append([0,0,0])
    

    这个错误应该会消失。

    【讨论】:

      【解决方案3】:

      您的代码中有一些错误,我在每次更正时都用 cmets 更正了它们。检查这个我认为这可以解决你的问题:-

      height=0   
      width=0
      device=0
      num_pieces=10
      
      # you need to provide initialisation with above fields in your program which is required
      
      pieces = [[height, width, device]]
      
      
      for i in range(num_pieces - 1):
          '''in your code line below returns null and when pieces become
             null it has no attribute append '''
            # pieces=pieces.append([0,0,0])   
          pieces.append([0,0,0])
      
      #print pieces
      
      j=0
      
      for i in pieces[:]:
         # get height of the ith piece
         #print "please enter the height of piece %d" 
         #height = float(raw_input(">>"))
      
         '''you can have user provided values
            i have hard coded the values for
            height width and device for the
            time being for simplicity '''
      
         height=10                                    
         # get width of the ith piece
         #print "please enter the width of piece %d" 
         #width = float(raw_input(">>"))
      
         width=10
         # get device of the ith piece
         # print "please enter the hanging device of piece %d"
         #device = float(raw_input(">>"))
      
         device=100
      
         # test if element is empty
         if i == [0,0,0]:
            i = [height, width, device]
      
         if j <= len(pieces):
            # in your code no need for (pieces.index(i)+1)
            pieces[j]=i
            j=j+1
      
      ##view completeled list of dimensions
      print pieces 
      

      【讨论】:

      • 谢谢!我对编码很陌生,这是我的第一个半难题。您的回答帮助很大!
      猜你喜欢
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 2020-02-03
      • 2020-01-28
      • 2017-07-02
      • 2016-12-05
      相关资源
      最近更新 更多