0

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

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

I might be misunderstanding the question but what I got is that they are looking to have every element in slist used with every element from elist. This will result in 1225 elements total in the final list. It appears that your function is returning 35.
@PeterH It is possible that OP wants an outer concatenation. Maybe OP will clarify later.
Yes, @Psidom is correct. I actually didn't know the term for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.