简体   繁体   English

在python中将字符串转换为二维数组

[英]Convert a string into 2D Array in python

I take an image from webcam in python, it is an array so I convert it to string using numpy.array_str() , now after doing that I need to convert that string back to an array as efficiently as possible.我从 python 中的网络摄像头获取图像,它是一个数组,所以我使用numpy.array_str()将其转换为字符串,现在我需要尽可能有效地将该字符串转换回数组。 I have already tried it using for statements but it takes around half a second, which is just too slow for my use-case.我已经尝试过使用 for 语句,但它需要大约半秒钟,这对于我的用例来说太慢了。 I have even searched the web for numpy's built-in function to do the task but did not find any solution.我什至在网上搜索了 numpy 的内置函数来完成任务,但没有找到任何解决方案。

The numpy.array_str() function returns a string with a whitespace separator between the array elements as https://numpy.org/doc/stable/reference/generated/numpy.array_str.html numpy.array_str() 函数返回一个字符串,在数组元素之间使用空格分隔符作为https://numpy.org/doc/stable/reference/generated/numpy.array_str.html

To convert it back to an array, you can use str.split() without specifying the delimiter because it considers whitespace as default.要将其转换回数组,您可以使用 str.split() 而不指定分隔符,因为它将空格视为默认值。

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

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