簡體   English   中英

在python中使用.append定義列表

[英]defining lists with .append in python

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 = pieces.append([0,0,0])

它將pieces的值設置為None因為列表append()方法有效地返回了該值。 大多數就地更改關聯容器對象的容器方法不會返回任何內容(Python使它們看起來好像已返回值None )。

在您的代碼中,這意味着在以下步驟的第一次迭代后, pieces的值更改為:

for i in range(num_pieces - 1)

…因此發生您看到的TypeError

將行更改為簡單:

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

而且該錯誤應該消失。

我不知道您要做什么,但是您的代碼中存在一些缺陷:

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

您的代碼中有一些錯誤,我在每次更正時都會用注釋進行更正。 檢查一下我認為這可以解決您的問題:-

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM