【发布时间】:2021-07-12 21:41:52
【问题描述】:
我在该领域工作。然后我将此字段用作列表。 我正在尝试将此列表放回数组 numpy,但我有一个问题,当我将字母插入 numpy 时,我会在那里得到不需要的引号。
可以以任何方式删除这些引号吗? 或者修改代码,这样我就不必将数组转换为列表,而是继续作为数组
我尝试使用替换来删除它们,但它不起作用。 你还有什么办法吗?
我的代码
import os
import numpy as np
from itertools import combinations
a = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
b = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
z1 = np.array([1, 1])
z2 = np.array([1, 1])
comb_x = np.array([0, 0, 1, 1])
comb_y = np.array([0, 1, 0, 1])
letsgo = [[] for i in range(4)]
go=np.array([])
for (j), (k) in zip(a, b):
z1[:] = 0
z1[:j] = 1
x12 = z1
z2[:] = 0
z2[:k] = 1
y12 = z2
for (h),(n),(r) in zip(comb_x,comb_y,np.arange(0,4)):
#print(h,n,'iteracia = ',r)
if x12[h] == 1 and y12[n] == 1:
go=np.append(go,x12,axis=0)
letsgo[r].append(f'{x12} {y12}')
for list in letsgo:
for string in list:
string = ', '.join(string.split())
#pole=pole.replace("'",'')
#print(string)
pole = np.array([string])
#pole=pole.replace("'",'')
print(pole)
我的输出:
['[1, 0], [1, 0]']
['[1, 0], [1, 1]']
['[1, 1], [1, 0]']
['[1, 1], [1, 1]']
['[1, 0], [1, 1]']
['[1, 1], [1, 1]']
['[1, 1], [1, 0]']
['[1, 1], [1, 1]']
['[1, 1], [1, 1]']
需要的输出:
[[1, 0], [1, 0]]
[[1, 0], [1, 1]]
[[1, 1], [1, 0]]
[[1, 1], [1, 1]]
[[1, 0], [1, 1]]
[[1, 1], [1, 1]]
[[1, 1], [1, 0]]
[[1, 1], [1, 1]]
[[1, 1], [1, 1]]
基本上,我希望它再次成为标准的 np.array。 你能给我建议吗?
【问题讨论】:
-
不想要字符串为什么要转成字符串?
-
当您执行
string = ', '.join(string.split())时,您正在将列表转换为字符串 -
无法直接用数组得到需要的输出,所以通过字符串来实现