我每三秒就会收到一些POST 数据
(正好是 384 行)。这些存储在名为 data
的列表中。然后我想将它们存储在列表 helper
中,该列表将在每次 POST 后 append 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
内容如下所示:
[[0.46124267578125, 0.0545654296875, 0.89111328125, 0.0, 0.0, 0.0, 0.0],
[0.46124267578125, 0.0545654296898, 0.89111328125, 0.0, 0.0, 0.0, 0.0],
[0.46124267578125, 0.0545654296875, 0.89111328125, 0.0, 0.0, 0.0, 0.0],
[0.4637451171875, 0.05804443359362, 0.8892822265625, 0.0, 0.0, 0.0, 0.0],
[0.4637451171875, 0.05804443359301, 0.8892822265625, 0.0, 0.0, 0.0, 0.0],
[0.4637451171875, 0.05804443359375, 0.8892822265625, 0.0, 0.0, 0.0, 0.0],
[etc.]]
我认为列表的尺寸存在问题,我无法弄清楚。
最佳答案
您有一个列表,您可以在其中 append 另一个列表,从而为您提供一个包含一项的嵌套列表。简单演示:
>>> data = [1,2,3]
>>> helper = []
>>> helper.append(data)
>>> helper
[[1, 2, 3]]
>>> len(helper)
1
从你的问题中我无法弄清楚为什么你需要 helper
列表,而是要制作一个(浅)复制问题 helper = data[:]
或helper.extend(数据)
。由于我不确定您从这里要去哪里,所以我将留下这个答案,告诉您为什么您的 helper
列表现在只有一个元素。
关于python - 在Python中追加列表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560447/