【发布时间】:2021-09-27 14:23:05
【问题描述】:
我有一个用 python 编写的 web 服务。它从 angular code.in 网络服务的 python 代码中调用,如下所示,网络服务返回 isKeyWindowSegmentRepresentative 这是一个布尔数组
.当从角度代码调用服务时,我收到isKeyWindowSegmentRepresentative 作为字符串。
我想将上述数组作为包含布尔值的数组接收,这样我就可以遍历它的内容。
Web 服务返回的内容和 Angular 代码显示的是以下字符串:
isKeyWindowSegmentRepresentative: "[false, false, true, true, false, false, true, true, true, true]"
我想将它作为一个布尔数组接收。
注意:
它不是一个 numpy 数组。它是一个普通数组,声明如下: isKeyWindowSegmentRepresentative=[]
更新:
for: "pixelsValuesSatisfyThresholdInWindowSegment":np.array(pixelsValuesSatisfyThresholdInWindowSegment,dtype=float).tolist()
i recieve the following error:
TypeError: float() argument must be a string or a number, not 'list'
the pixelsValuesSatisfyThresholdInWindowSegment contains _pixelsValuesSatisfyThresholdInWindowSegment
logger.debug(type(pixelsValuesSatisfyThresholdInWindowSegment)):<class 'list'>
logger.debug(type(pixelsValuesSatisfyThresholdInWindowSegment[0])):<class 'list'>
logger.debug(type(_pixelsValuesSatisfyThresholdInWindowSegment)):<class 'list'>
logger.debug(type(_pixelsValuesSatisfyThresholdInWindowSegment[0]))::<class 'numpy.float32'>
从 python 代码返回字典为 json:
resultsDict = {
"isKeyWindowSegmentRepresentative":json.dumps(np.array(isKeyWindowSegmentRepresentative, dtype=bool).tolist())
}
【问题讨论】:
-
你为什么使用 numpy (
np)? -
那么为什么要使用
np.array(...).tolist()?从列表到 numpy 数组再到列表似乎是循环的。 -
@ogdenkev 因为我收到 bool_ 类型的对象不是 JSON 可序列化的