简体   繁体   English

Python:从 numpy.ndarray 转换为列表:挂起

[英]Python : conversion from numpy.ndarray to list : getting hung

I have a list named word_vector whose every element is of type 'numpy.ndarray'.我有一个名为word_vector的列表,其每个元素的类型都是“numpy.ndarray”。

print(word_vector)

output: output:

[array([0., 0., 0., ..., 0., 0., 0.]), array([0., 0., 0., ..., 0., 0., 0.]), array([0., 0., 0., ..., 0., 0., 0.]), array([0., 0., 0., ..., 0., 0., 0.])]

I want to convert the type of each element of the list to list.我想将列表中每个元素的类型转换为列表。 So I wrote this code:所以我写了这段代码:

word_vector_list = []
for arr in word_vector:
    list_ = arr.tolist()
    word_vector_list.append(list_)
    
print(word_vector_list) 

The programming is getting hung repeatedly.程序反复挂起。 I'm getting this exception:我得到了这个例外:

IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.

Current values:
NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
NotebookApp.rate_limit_window=3.0 (secs)

length of each element(of type array) of the list is:列表中每个元素(数组类型)的长度为:

print(len(arr))

output: output:

4395

I'm not entirely sure why your program is hanging.我不完全确定您的程序为何挂起。 Try using list comprehension like so, it should be better:尝试像这样使用列表理解,它应该会更好:

word_vector_list = [list(x) for x in word_vector]

If it still didn't work, try increasing the iopub data rate.如果它仍然不起作用,请尝试增加 iopub 数据速率。 To increase the iopub data rate, run this in the terminal:要提高 iopub 数据速率,请在终端中运行:

jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10

Hope this helps:)希望这可以帮助:)

2d np array can be changed to list of lists directly by.tolist function. 2d np 数组可以直接通过.tolist function 更改为列表列表。

Also seen here也看到这里

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM