如何通过连接2个数组来创建数组?

时间:2017-10-20 23:49:05

标签: python arrays numpy

我有这两个数组

$students = $usersRepository->getStudentsForProfessor($professor, 'name', 'ASC');

$students = $professor->getStudents()->orderBy('name')->get();

给了我:

slist = np.arange(1,1723,50) #List of start index
elist = np.arange(50,1800,50) #End index

我想对 'G'+'{:0>5}'.format(slist[0])+"-"+'{:0>5}'.format(elist[0]) 'G00001-00050' 的所有元素执行此操作。

我该怎么做?

我的想法是构建一个数组列表,我将用它来导入文件,这些文件将它们作为文件名中的字符串

1 个答案:

答案 0 :(得分:1)

您可以使用np.char.zfill0填充到字符串中,并使用np.char.add将它们连接起来:

from functools import reduce
import numpy as np
​
def cust_format(slist, elist):

    slist = np.char.zfill(slist.astype(str), 5)
    elist = np.char.zfill(elist.astype(str), 5)
    lst = ['G', slist, '-', elist]

    return reduce(np.char.add, lst)

cust_format(slist, elist)
#array(['G00001-00050', 'G00051-00100', 'G00101-00150', 'G00151-00200',
#       'G00201-00250', 'G00251-00300', 'G00301-00350', 'G00351-00400',
#       'G00401-00450', 'G00451-00500', 'G00501-00550', 'G00551-00600',
#       'G00601-00650', 'G00651-00700', 'G00701-00750', 'G00751-00800',
#       'G00801-00850', 'G00851-00900', 'G00901-00950', 'G00951-01000',
#       'G01001-01050', 'G01051-01100', 'G01101-01150', 'G01151-01200',
#       'G01201-01250', 'G01251-01300', 'G01301-01350', 'G01351-01400',
#       'G01401-01450', 'G01451-01500', 'G01501-01550', 'G01551-01600',
#       'G01601-01650', 'G01651-01700', 'G01701-01750'], 
#      dtype='|S12')