numpy | 插入不定长字符数组测试OK

Posted yantuguiguziPGJ

tags:

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

测试代码

import numpy.matlib
import numpy as np
import sys

numpy.set_printoptions(threshold=sys.maxsize)

print("\\n********************************************OK*************************************")

data_type='names':('name', 'a', 'b'), 'formats':('S100', 'S100','S100')
data1=np.rec.array([('http',"1","1")],dtype=data_type)
data2=np.rec.array([('sjtv-bid-hoisin.coocaa.com/v1/bid','content-length','ghi')],dtype=data_type)
print(data1) 
print("\\n")  
print(data2) 
print("\\n")
result = np.insert(data1, 1, data2, axis = 0)
print(result) 

print("\\n********************************************OK END*************************************")

print("\\n********************************************以下为瞎试  插入字符数组被截断测试*************************************")

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

********************************************OK*************************************
[(b'http', b'1', b'1')]


[(b'sjtv-bid-hoisin.coocaa.com/v1/bid', b'content-length', b'ghi')]


[(b'http', b'1', b'1')
 (b'sjtv-bid-hoisin.coocaa.com/v1/bid', b'content-length', b'ghi')]

********************************************OK END*************************************

********************************************以下为瞎试  插入字符数组被截断测试*************************************

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 | 插入不定长字符数组测试OK的主要内容,如果未能解决你的问题,请参考以下文章

STL map vector(不定长数组)

不定长数组:vector

C语言格式化输入不定长数组

不定长参数作用

结构体成员数组不定长如何实现

(STL初步)不定长数组:vector