Questions tagged [concatenation]

Joining of two or more sequences into a single sequence.

Concatenation refers to the joining of two or more elements into a single element.

In programming, this can refer to

  • concatenation, which joins multiple strings into a single string
  • concatenation, where one or more arrays are combined (mostly appended) to make a new array
  • specific concatenation functions that combine elements along a specified dimension (horizontally, or vertically for example), replicate and tile existing elements, and so forth.
12037 questions
95
votes
11 answers

Comma separated string of selected values in MySQL

I want to convert selected values into a comma separated string in MySQL. My initial code is as follows: SELECT id FROM table_level WHERE parent_id = 4; Which produces: '5' '6' '9' '10' '12' '14' '15' '17' '18' '779' My desired output would look…
Karunakar
  • 2,209
  • 4
  • 15
  • 20
94
votes
8 answers

Concatenating two range function results

Does range function allows concatenation ? Like i want to make a range(30) & concatenate it with range(2000, 5002). So my concatenated range will be 0, 1, 2, ... 29, 2000, 2001, ... 5001 Code like this does not work on my latest python (ver:…
MAG
  • 2,841
  • 6
  • 27
  • 47
94
votes
2 answers

Function to concatenate paths?

Is there an existing function to concatenate paths? I know it is not that difficult to implement, but still... besides taking care of trailing / (or \) I would need to take care of proper OS path format detection (i.e. whether we write C:\dir\file…
Adam Ryczkowski
  • 7,592
  • 13
  • 42
  • 68
93
votes
4 answers

Python: Concatenate (or clone) a numpy array N times

I want to create an MxN numpy array by cloning a Mx1 ndarray N times. Is there an efficient pythonic way to do that instead of looping? Btw the following way doesn't work for me (X is my Mx1 array) : numpy.concatenate((X, numpy.tile(X,N))) since…
stelios
  • 2,679
  • 5
  • 31
  • 41
91
votes
13 answers

is the + operator less performant than StringBuffer.append()

On my team, we usually do string concatentation like this: var url = // some dynamically generated URL var sb = new StringBuffer(); sb.append("click here"); Obviously the following is much more readable: var…
Dónal
  • 185,044
  • 174
  • 569
  • 824
91
votes
6 answers

How do I concatenate a boolean to a string in Python?

I want to accomplish the following answer = True myvar = "the answer is " + answer and have myvar's value be "the answer is True". I'm pretty sure you can do this in Java.
travis1097
  • 2,829
  • 5
  • 23
  • 23
87
votes
9 answers

Most optimized way of concatenation in strings

We always came across many situation on daily basis wherein we have to do tedious and very many string operations in our code. We all know that string manipulations are expensive operations. I would like to know which is the least expensive among…
Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
86
votes
4 answers

Concatenate strings in python in multiline

I have some strings to be concatenated and the resultant string will be quite long. I also have some variables to be concatenated. How can I combine both strings and variables so the result would be a multiline string? The following code throws…
user3290349
  • 1,227
  • 1
  • 9
  • 17
86
votes
3 answers

Pandas column bind (cbind) two data frames

I've got a dataframe df_a with id information: unique_id lacet_number 15 5570613 TLA-0138365 24 5025490 EMP-0138757 36 4354431 DXN-0025343 and another dataframe df_b, with the same number of rows that I know correspond to the rows…
breezymri
  • 3,975
  • 8
  • 31
  • 65
86
votes
4 answers

Concatenate two NumPy arrays vertically

I tried the following: >>> a = np.array([1,2,3]) >>> b = np.array([4,5,6]) >>> np.concatenate((a,b), axis=0) array([1, 2, 3, 4, 5, 6]) >>> np.concatenate((a,b), axis=1) array([1, 2, 3, 4, 5, 6]) However, I'd expect at least that one result looks…
toom
  • 12,864
  • 27
  • 89
  • 128
85
votes
3 answers

pandas concat generates nan values

I am curious why a simple concatenation of two dataframes in pandas: initId.shape # (66441, 1) initId.isnull().sum() # 0 ypred.shape # (66441, 1) ypred.isnull().sum() # 0 of the same shape and both…
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
82
votes
6 answers

String concatenation without '+' operator

I was playing with python and I realized we don't need to use '+' operator to concatenate static strings. But it fails if I assign it to a variable. For example: string1 = 'Hello' 'World' #1 works fine string2 = 'Hello' + 'World' #2 also works…
ibrahim
  • 3,254
  • 7
  • 42
  • 56
81
votes
3 answers

Ruby: How to concatenate array of arrays into one

I have an array of arrays in Ruby on Rails (3.1) where all the internal arrays are of different size. Is there a way to easily concatenate all the internal arrays to get one big one dimesional array with all the items? I know you can use the…
Pedro Cori
  • 2,046
  • 2
  • 16
  • 23
80
votes
11 answers

Can't concatenate 2 arrays in PHP

I've recently learned how to join 2 arrays using the + operator in PHP. But consider this code... $array = array('Item 1'); $array += array('Item 2'); var_dump($array); Output is array(1) { [0]=> string(6) "Item 1" } Why does this not…
alex
  • 479,566
  • 201
  • 878
  • 984
79
votes
7 answers

Pandas concat yields ValueError: Plan shapes are not aligned

In pandas, I am attempting to concatenate a set of dataframes and I am getting this error: ValueError: Plan shapes are not aligned My understanding of .concat() is that it will join where columns are the same, but for those that it can't find it…
Lt.Fr0st
  • 999
  • 1
  • 7
  • 10