maximusdooku
maximusdooku

Reputation: 5512

How can I create an array by concatenating from 2 arrays?

I have these two arrays

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

This

   'G'+'{:0>5}'.format(slist[0])+"-"+'{:0>5}'.format(elist[0])

gives me:

    'G00001-00050'

I want to do this over all the elements of slist and elist.

How can I do this?

The idea is to construct a array list which I'll use to import files which have them as strings in file names

Upvotes: 0

Views: 54

Answers (1)

akuiper
akuiper

Reputation: 214957

You can use np.char.zfill to pad 0 to strings and np.char.add to concatenate them element-wise:

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')

Upvotes: 1

Related Questions