Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用.append定义列表_Python_List_Loops_Append_Typeerror - Fatal编程技术网

在python中使用.append定义列表

在python中使用.append定义列表,python,list,loops,append,typeerror,Python,List,Loops,Append,Typeerror,我认为我的.append(我不应该使用.extend我应该吗?) 这是我的错误。我知道[1:://code>也有问题,但首先我想找出如何解决.append问题 文件“l_long.py”,第36行,在 因为我是碎片[1:]: TypeError:“非类型”对象没有属性“\uuuu getitem\uuuu” 我不知道您想做什么,但您的代码中有一些缺陷: height, width, device = 99, 99, 99 num_pieces = 3 #create datastructure

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

这是我的错误。我知道
[1:://code>也有问题,但首先我想找出如何解决.append问题

文件“l_long.py”,第36行,在
因为我是碎片[1:]:
TypeError:“非类型”对象没有属性“\uuuu getitem\uuuu”

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

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

您得到的错误是因为这一行:

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
它将
pieces
的值设置为
None
,因为list
append()
方法有效地返回该值。大多数更改关联容器对象的容器方法都不会返回任何内容(Python使其看起来好像返回了值
None

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

pieces = pieces.append([0,0,0])
…因此出现了您看到的
TypeError

将行更改为:

for i in range(num_pieces - 1)

这个错误应该会消失。

您的代码中有一些错误,我已经在每次更正时用注释更正了它们。检查这一个,我认为这解决了您的问题:-

pieces.append([0,0,0])
height=0
宽度=0
设备=0
件数=10件
#您需要在程序中提供上述字段的初始化,这是必需的
件数=[[高度、宽度、设备]]
对于范围内的i(个数-1):
下面代码行中的“”返回null,当片段变为空时
null它没有附加属性“”
#片段=片段。追加([0,0,0])
片段。追加([0,0,0])
#印刷品
j=0
因为我是碎片[:]:
#获取第i块的高度
#打印“请输入工件%d的高度”
#高度=浮动(原始输入(“>>”)
''您可以拥有用户提供的值
我已经硬编码了
高度、宽度和设备
“为了简单起见,现在是时候了”
高度=10
#获取第i块的宽度
#打印“请输入工件%d的宽度”
#宽度=浮动(原始输入(“>>”)
宽度=10
#获取第i块的设备
#打印“请输入工件%d的悬挂装置”
#设备=浮点(原始输入(“>>”)
设备=100
#测试元素是否为空
如果i==[0,0,0]:
i=[高度、宽度、设备]

如果j请修复代码中的缩进,然后我们可以关注其他各种问题。另外,如果您给我们一些您希望此代码打印的典型输出,这将是一件好事<代码>件。索引(i)+1
您可以直接使用
件[0][i]
谢谢!我对编码很陌生,这是我的第一个半难题。你的回答帮了大忙!
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