Element-Wise Concatenation of String Vectors

element-wise concatenation of string vectors

You can use paste or paste0:

> a <- c("a", "b", "c")
> b <- c("1", "2", "3")
> paste0(a, b)
[1] "a1" "b2" "c3"
>

Element-wise string concatenation in numpy

This can be done using numpy.core.defchararray.add. Here is an example:

>>> import numpy as np
>>> a1 = np.array(['a', 'b'])
>>> a2 = np.array(['E', 'F'])
>>> np.core.defchararray.add(a1, a2)
array(['aE', 'bF'],
dtype='<U2')

There are other useful string operations available for NumPy data types.

Elementwise concatenation of string arrays

You could reduce an array with the single arrays. This works for more than one array as well.

var inputArray1 = ['abc', 'def', 'ghi'],

inputArray2 = ['3', '6', '9'],

outputArray = [inputArray1, inputArray2].reduce((a, b) => a.map((v, i) => v + ' - ' + b[i]));

console.log(outputArray);

Elementwise Concatenation of vectors in R

Paste0 should do the job:

output = paste0(a,b)

Or more general paste if you want to add a seperator between the strings:

output = paste(a, b, sep="-")

Element-wise concatenation of 2 vectors in kdb+

You could also instantiate through the following

(A;B) to create a 2x3 matrix which can be flipped to get what you require

q)A:0 1 2
q)B:3 4 5
q)(A;B)
0 1 2
3 4 5
q)flip (A;B)
0 3
1 4
2 5

Elementwise concatenation in numpy

One way would be stacking replicated versions created with np.repeat and np.tile -

In [52]: n = len(a)

In [53]: np.hstack((np.repeat(a,n,axis=0),np.tile(a,(n,1))))
Out[53]:
array([[0, 1, 0, 1],
[0, 1, 2, 3],
[0, 1, 4, 5],
[2, 3, 0, 1],
[2, 3, 2, 3],
[2, 3, 4, 5],
[4, 5, 0, 1],
[4, 5, 2, 3],
[4, 5, 4, 5]])

Another would be with broadcasted-assignment, since you mentioned broadcasting -

def create_mesh(a):
m,n = a.shape
out = np.empty((m,m,2*n),dtype=a.dtype)
out[...,:n] = a[:,None]
out[...,n:] = a
return out.reshape(-1,2*n)

How to concatenate element-wise two lists in Python

Use zip:

>>> ["{}{:02}".format(b_, a_) for a_, b_ in zip(a, b)]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']

Concatenate multiple numpy string arrays with a space delimiter

Try np.apply_along_axis

arr_list = [strings1, strings2, strings3]
arr_out = np.apply_along_axis(' '.join, 0, arr_list)

In [35]: arr_out
Out[35]: array(['a d g', 'b e h', 'c f i'], dtype='<U5')

Element wise concatenate multiple lists (list of list of strings)

Here's one way zipping the sublists and mapping with ''.join the resulting tuples:

list(map(''.join, zip(*lst)))
# ['a@1', 'b$2', 'c#3']

Here zip as shown in the docs aggregates elements from several iterables. With *, we are unpacking the list into separate iterables, which means that the function will instead be receiving zip(['a','b','c'],['@','$','#'],['1','2','3']).

Now at each iteration, map will be applying ''.join to each of the aggregated iterables, i.e to the first element in each sublist, then the second, and so on.



Related Topics



Leave a reply



Submit