numpy | 插入字符数组被截断测试

Posted yantuguiguziPGJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了numpy | 插入字符数组被截断测试相关的知识,希望对你有一定的参考价值。

测试代码

import numpy.matlib
import numpy as np
import sys

numpy.set_printoptions(threshold=sys.maxsize)

Z = np.array([("Hello1234567890", 2.51234567890, 3),
              ("World", 3.6, 2)])
R8 = np.core.records.fromarrays(Z.T,
                               names='col1, col2, col3',
                               formats = 'S8, f8, i8')
print("\\nR8--->formats = 'S8, f8, i8':   ")
print(R8)
R15 = np.core.records.fromarrays(Z.T,
                               names='col1, col2, col3',
                               formats = 'S15, f8, i8')
print("\\nR15--->formats = 'S15, f8, i8':   ")
print(R15)

# A = np.ones(3)*1
# B = np.ones(3)*2
# np.add(A,B,out=B)
# np.divide(A,2,out=A)
# np.negative(A,out=A)
# np.multiply(A,B,out=A)


a = np.array([['http',1,1]])
print ("\\na:   ")
print(a)
# a = np.array([['http',1,1]], 
#             names='col1, col2, col3',
#             formats = 'S15, S15, S15')

# a = np.array([['http',1,1]],dtype = np.string_)  

ccc = np.array(['abc','def','ghi'])
print ("\\nccc:   ")
print(ccc)
print (ccc.itemsize)
print ("\\nnp.insert(a, 1, ccc, axis = 0):   ")
print(np.insert(a, 1, ccc, axis = 0))

ddd = np.array(['sjtv-bid-hoisin.coocaa.com/v1/bid','content-length','ghi'])
print ("\\nddd:   ")
print(ddd)
print (ddd.itemsize)
print ("\\nnp.insert(a, 1, ddd, axis = 0):   ")
print(np.insert(a, 1, ddd, axis = 0))

eee = np.array(['sjtv-bid','content-length','ghi'])
print ("\\neee:   ")
print(eee)
print (eee.itemsize)
print ("\\nnp.insert(a, 1, eee, axis = 0):   ")
print(np.insert(a, 1, eee, axis = 0))

fff = np.array([['sjtv-bid-hoisin.coocaa.com/v1/bid','content-length','ghi']])
print ("\\nfff:   ")
print(fff)
print (fff.itemsize)
print ("\\nnp.insert(a, 1, fff, axis = 0):   ")
result = np.insert(a, 1, fff, axis = 0)
print(result)

resultR = np.core.records.fromarrays(result.T,
                               names='col1, col2, col3',
                               formats = 'S15, S15, S15')
print ("\\nresultR--->np.core.records.fromarrays:   ")                  
print(resultR)

测试结果

PS C:\\Users\\pgjgg\\Desktop\\python_learn> & C:/Users/pgjgg/AppData/Local/Programs/Python/Python39/python.exe c:/Users/pgjgg/Desktop/python_learn/numpy_demo.py

R8--->formats = 'S8, f8, i8':
[(b'Hello123', 2.51234568, 3) (b'World', 3.6       , 2)]

R15--->formats = 'S15, f8, i8':
[(b'Hello1234567890', 2.51234568, 3) (b'World', 3.6       , 2)]

a:
[['http' '1' '1']]

ccc:
['abc' 'def' 'ghi']
12

np.insert(a, 1, ccc, axis = 0):
[['http' '1' '1']
 ['abc' 'def' 'ghi']]

ddd:
['sjtv-bid-hoisin.coocaa.com/v1/bid' 'content-length' 'ghi']
132

np.insert(a, 1, ddd, axis = 0):
[['http' '1' '1']
 ['sjtv-bid-ho' 'content-le' 'ghi']]

eee:
['sjtv-bid' 'content-length' 'ghi']
64

np.insert(a, 1, eee, axis = 0):
[['http' '1' '1']
 ['sjtv-bid' 'content-le' 'ghi']]

fff:
[['sjtv-bid-hoisin.coocaa.com/v1/bid' 'content-length' 'ghi']]
132

np.insert(a, 1, fff, axis = 0):
[['http' '1' '1']
 ['sjtv-bid-ho' 'content-len' 'ghi']]

resultR--->np.core.records.fromarrays:
[(b'http', b'1', b'1') (b'sjtv-bid-ho', b'content-len', b'ghi')]

以上是关于numpy | 插入字符数组被截断测试的主要内容,如果未能解决你的问题,请参考以下文章

在numpy中将int数组转换为字符串数组而不截断

如何截断一个numpy数组?

Laravel Eloquent 对象,长文本被截断

如何截断大于指定值的值的numpy数组?

SQLException : 字符串或二进制数据将被截断

尝试插入时,错误、字符串或二进制数据将被截断