2

I want to concatenate a simple string with the values of a numpy array.

I have create a new numpy array with np.arrange to get a incremental list of number [0, 1, 2]. But I am not able to concatenate each number with a string "User".

I tried to concatenate with the numpy function concatenate and defchararray without success. It seems like the concatenate function only concatenate two numpy arrays but not concatenate a string and an array.

Here is the code:

Anon_id = np.arange(10)

print np.core.defchararray.add('User',Anon_id)
print np.concatenate('User',Anon_id)

Can you help me figure out a solution, please ?

Best regards.

3
  • 1
    does this have to be done a) in python2 and b) in numpy? in python3, that could simply be users = [f'User{i}' for i in range(10)] Commented Nov 22, 2019 at 10:59
  • The environnement is in python2, I prefer a solution with numpy. jdehesa solution seems better because there are no loop specify in the code. Thanks. Commented Nov 22, 2019 at 11:06
  • "no loops visible" is not necessarily better imho (a loop will be running somewhere anyway). Added a timeit comparison for reference. Anyways, jdehesa's solution is nice, so +1 Commented Nov 22, 2019 at 11:30

4 Answers 4

3

You have to convert the integer array to strings:

import numpy as np
out = np.core.defchararray.add('User', np.arange(10).astype(str))
print(out)
# ['User0' 'User1' 'User2' 'User3' 'User4' 'User5' 'User6' 'User7' 'User8'
#  'User9']
Sign up to request clarification or add additional context in comments.

Comments

2

How about something like this:

np.array(['User{}'.format(i) for i in range(10)])

Comments

2

A little %timeit comparison for reference:

%timeit [f'User{i}' for i in range(10)]
2.39 µs ± 106 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit np.core.defchararray.add('User', np.arange(10).astype(str))
23.9 µs ± 1.85 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

and for larger arrays

%timeit [f'User{i}' for i in range(1000)]
214 µs ± 8.73 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit np.core.defchararray.add('User', np.arange(1000).astype(str))
1.18 ms ± 15.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Can't check for Python2 right now but native Python3 (3.7.5, 64bit) is hard to beat here! For large array sizes, the performance advantage of native Python roughly converges to x6.

Comments

1
import numpy as np

# create a list
Anon_id = list(map(str, np.arange(10)))

# join User string in a different step
print(np.core.defchararray.add('User', Anon_id))

## ['User0' 'User1' 'User2' 'User3' 'User4' 'User5' 'User6' 'User7' 'User8' 'User9']

Comments

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.