gpt4 book ai didi

python - 将数组形状转换为字符串

转载 作者:行者123 更新时间:2023-12-01 05:13:24 25 4
gpt4 key购买 nike

我需要将 2D 数组的 myarray.shape 的输出转换为字符串,因为我想隔离行和列,并将它们重新分配为我已经生成的图像的高度和宽度读入,不使用 PIL。

我尝试了(str)image1.shape,但它只是给出了语法错误。

正确的做法是什么?

最佳答案

这是 str(image1.shape) 。如果您想解析它(假设它是 (50,2)),您可以这样做:

myshape = str(image1.shape)  # returns '(50, 2)'
part1, part2 = myshape.split(', ')
part1 = part1[1:] # now is '50'
part2 = part2[:-1] # now is '2'

或者,因为你真的在追求数字(我认为),所以只需跳过 str() 步骤并直接解析 image1 的输出。形状:

firstnum, secondnum = image1.shape

你就完成了。

关于python - 将数组形状转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23708288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com