在Python中追加列表的问题
我每三秒收到一些在Python中追加列表的问题,python,list,numpy,append,Python,List,Numpy,Append,我每三秒收到一些POST数据(准确地说是384行)。这些数据存储在名为数据的列表中。然后我想将它们存储在listhelper中,在每篇文章之后都会添加data。现在我想检查图形中的数据,所以我需要将helper转换为numpy数组,称为myArr data = json.loads(json_data)["data"] #I get some data helper=[] #Then create list helper.append(data) # And here I would like
POST数据
(准确地说是384行)。这些数据存储在名为数据的列表中。然后我想将它们存储在listhelper
中,在每篇文章之后都会添加data
。现在我想检查图形中的数据,所以我需要将helper
转换为numpy数组,称为myArr
data = json.loads(json_data)["data"] #I get some data
helper=[] #Then create list
helper.append(data) # And here I would like to add values to the end
myArr=np.asarray(helper)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("")
print (len(data))
print(type (data))
print (len(helper))
print(type (helper))
print (len(myArr))
print(type (myArr))
print data
但当我执行代码时,长度不同:
>>384
>><type 'list'>
>>1
>><type 'list'>
>>1
>><type 'numpy.ndarray'>
我认为列表的维度有问题,我无法理解。您有一个列表,在该列表中附加了另一个列表,给您一个带有一个项目的嵌套列表。简单演示:
>>> data = [1,2,3]
>>> helper = []
>>> helper.append(data)
>>> helper
[[1, 2, 3]]
>>> len(helper)
1
我无法从你的问题中理解为什么你需要helper
列表,而是需要(浅层)复制问题helper=data[:]
或helper.extend(data)
。因为我不确定你要从这里走到哪里,我将留下这个答案,告诉你为什么你的helper
列表现在只有一个元素。如果你想连接列表,你可以简单地做list\u c=list\u a+list\u b
或者在你的情况下helper+=data
谢谢你的回答,我明白了,我正在0中创建某种金字塔。名单索引:D
>>> data = [1,2,3]
>>> helper = []
>>> helper.append(data)
>>> helper
[[1, 2, 3]]
>>> len(helper)
1