2024-04-23 16:49:11 发布
网友
我有一个字符串,它是numpy数组的列表。 绳子看起来像
k = '[array([ 0, 269, 175, 377]), array([ 0, 56, 149, 163])]'
当我这么做的时候
我得到一个错误,说节点或字符串格式不正确。在
这里有什么问题?有没有更好的方法把它转换回列表?在
谢谢你的帮助!!在
我不确定这是不是一个好办法。在
from numpy import array import ast import re k = '[array([ 0, 269, 175, 377]), array([ 0, 56, 149, 163])]' val = re.findall(r"\((.*?)\)", k) val = list(map(ast.literal_eval, val)) val = list(map(array, val)) print(val)
输出:
()
ast.literal_eval
np.array
从^{} doc:
The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.
这里不可能使用文字求值。首先找到这些字符串的生成位置,然后在那里实现适当的序列化,例如使用^{}。在
我不确定这是不是一个好办法。在
输出:
^{pr2}$()
之间的内容ast.literal_eval
np.array
从^{} doc :
这里不可能使用文字求值。首先找到这些字符串的生成位置,然后在那里实现适当的序列化,例如使用^{} 。在
相关问题 更多 >
编程相关推荐