我有这个数组:
z = np.array(['43', '65', '41', '47', '46', '73', '99', '52', '56', '23', '07',
'C3', '49', '62', '54', 'A1', '88', '70', '42', 74.0, 20.0, 21.0,
4, 62, 2, 3, 49, 79, '13', 'F4', 'A9', '20', '19', 19.0, 23.0,
70.0, 83, 61, 80, 81, 66, 82, 63, '09', '06', 'F8'], dtype=object)
在此数组中,我们在一个数组中具有int,str和float。我想将它们全部转换为字符串,但浮点值必须为整数,并且诸如“ 07”,“ 09”等值也将变为“ 7”,“ 9”。我想要的欲望结果是:
z = np.array(['43', '65', '41', '47', '46', '73', '99', '52', '56', '23', '7',
'C3', '49', '62', '54', 'A1', '88', '70', '42', '74', '20', '21',
'4', '62', '2', '3', '49', '79', '13', 'F4', 'A9', '20', '19', '19', '23',
'70', '83', '61', '80', '81', '66', '82', '63', '9', '6', 'F8'], dtype=object)
我尝试过这种方法
def col_convert(array):
for i in range(len(array)):
try:
array[i] = str(int(array[i]))
except:
next
return array
但是,对于一百万个元素,此解决方案相当慢。有什么更快的方法来完成这项任务吗?
答案 0 :(得分:3)
尝试:
z1 = np.array([str(i).split('.')[0] for i in z])
更新:根据OP的操作删除前导零:
z1 = np.array([str(i).lstrip('0').split('.')[0] for i in z])