Hướng dẫn dùng concatonate python

Hàm concatenate () là một hàm từ gói NumPy. Về cơ bản, hàm này kết hợp các mảng NumPy với nhau. Hàm này về cơ bản được sử dụng để nối hai hoặc nhiều mảng có cùng hình dạng dọc theo một trục được chỉ định. Có những điều cần thiết sau đây cần ghi nhớ:

NumPy’s concatenate () không giống như một phép nối cơ sở dữ liệu truyền thống. Nó giống như việc xếp chồng các mảng NumPy.

Các bài viết liên quan:

Chức năng này có thể hoạt động theo cả chiều dọc và chiều ngang. Điều này có nghĩa là chúng ta có thể nối các mảng với nhau theo chiều ngang hoặc chiều dọc.

Hàm concatenate () thường được viết dưới dạng np.concatenate (), nhưng chúng ta cũng có thể viết nó dưới dạng numpy.concatenate (). Nó phụ thuộc vào cách nhập gói numpy, nhập numpy dưới dạng np hoặc nhập numpy, tương ứng.

Cú pháp

numpy.concatenate ((a1, a2, ...), axis)

Parameter

  1. (a1, a2, …)

Tham số này xác định trình tự của mảng. Ở đây, a1, a2, a3 … là các mảng có hình dạng giống nhau, ngoại trừ chiều tương ứng với trục.

  1. axis: int (tùy chọn)

Tham số này xác định trục mà mảng sẽ được nối với nhau. Theo mặc định, giá trị của nó là 0.

Nó sẽ trả về một ndarray chứa các phần tử của cả hai mảng.

Xem thêm Duyệt mảng trong NumPy

Ví dụ 1: numpy.concatenate ()

import numpy as np  
x=np.array([[1,2],[3,4]])  
y=np.array([[12,30]])  
z=np.concatenate((x,y))  
z  

Trong đoạn code trên

  • Chúng tôi đã tạo một mảng ‘x’ bằng cách sử dụng hàm np.array ().
  • Sau đó, chúng ta đã tạo một mảng khác ‘y’ bằng cách sử dụng cùng một hàm np.array ().
  • Chúng tôi đã khai báo biến ‘z’ và gán giá trị trả về của hàm np.concatenate ().
  • Chúng ta đã chuyển mảng ‘x’ và ‘y’ trong hàm.
  • Cuối cùng, chúng tôi đã in giá trị của ‘z’.

Trong đầu ra, giá trị của cả hai mảng, tức là ‘x’ và ‘y’ được hiển thị theo trục = 0.

Đầu ra:

Ví dụ 2: numpy.concatenate () với axis = 0

import numpy as np  
x=np.array([[1,2],[3,4]])  
y=np.array([[12,30]])  
z=np.concatenate((x,y), axis=0)  
z  

Output:

Ví dụ 3: numpy.concatenate () với axis = 1

import numpy as np  
x=np.array([[1,2],[3,4]])  
y=np.array([[12,30]])  
z=np.concatenate((x,y.T), axis=1)  
z  

Output:

Trong ví dụ trên, ‘.T’ được sử dụng để thay đổi các hàng thành cột và cột thành hàng.

Xem thêm Sử dụng Linear Algebra trong Numpy

Ví dụ 4: numpy.concatenate () với axis = None

import numpy as np  
x=np.array([[1,2],[3,4]])  
y=np.array([[12,30]])  
z=np.concatenate((x,y), axis=None)  
z 

Output:

Trong các ví dụ trên, chúng ta đã sử dụng hàm np.concatenate (). Chức năng này không được bảo toàn che các đầu vào MaskedArray. Có một cách sau đây mà qua đó chúng ta có thể nối các mảng có thể duy trì việc che các đầu vào MaskedArray.

Ví dụ 5: np.ma.concatenate ()

import numpy as np  
x=np.ma.arange(3)  
y=np.arange(3,6)  
x[1]=np.ma.masked  
x  
y  
z1=np.concatenate([x,y])  
z2=np.ma.concatenate([x,y])  
z1  
z2  

Trong đoạn code trên

  • Chúng tôi đã tạo một mảng ‘x’ bằng cách sử dụng hàm np.ma.arrange ().
  • Sau đó, chúng ta đã tạo một mảng ‘y’ khác bằng cách sử dụng cùng một hàm np.ma.arrange ().
  • Chúng tôi đã khai báo biến ‘z1’ và gán giá trị trả về của hàm np.concatenate ().
  • Chúng tôi đã khai báo biến ‘z2’ và gán giá trị trả về của hàm np.ma.concatenate ().
  • Cuối cùng, chúng tôi đã cố gắng in giá trị của ‘z1’ và ‘z2’.

Xem thêm Function trong R, các hàm trong R

String concatenation means add strings together.

Python Variables Tutorial Creating Variables Variable Names Assign Value to Multiple Variables Output Variables Global Variables

Copyright ©2022 Educative, Inc. All rights reserved

Summary: in this tutorial, you’ll learn various ways to concatenate strings in Python.

Python provides you with various ways to concatenate one or more strings into a new string.

Since Python string is immutable, the concatenation always results in a new string.

1) Concatenating literal strings

To concatenate two or more literal strings, you just need to place them next to each other. For example:

s = 'String' ' Concatenation' print(s)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

Note that this way won’t work for the string variables.

2) Concatenating strings using the + operator

A straightforward way to concatenate multiple strings into one is to use the + operator:

s = 'String' + ' Concatenation' print(s)

Code language: PHP (php)

And the + operator works for both literal strings and string variables. For example:

s1 = 'String' s2 = s1 + ' Concatenation' print(s2)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

3) Concatenating strings using the += operator

Similar to the + operator, you can use the += operator to concatenate multiple strings into one:

s = 'String' s += ' Concatenation' print(s)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

4) Concatenating strings using the join() method

The join() method allows you to concatenate a list of strings into a single string:

s1 = 'String' s2 = 'Concatenation' s3 = ''.join([s1, s2]) print(s3)

Code language: PHP (php)

Output:

StringConcatenation

The join() method also allows you to specify a delimiter when concatenating strings. For example:

s1 = 'String' s2 = 'Concatenation' s3 = ' '.join([s1, s2]) print(s3)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

In this example, we use the join() method to concatenate strings delimited by a space.

The following example use the join() method to concatenate strings delimited by a comma:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = ','.join([s1, s2, s3]) print(s)

Code language: PHP (php)

Output:

Python,String,Concatenation

Code language: JavaScript (javascript)

5) Concatenating strings using the %-formatting

String objects have the built-in % operator that allows you to format strings. Also, you can use it to concatenate strings. For example:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = '%s %s %s' % (s1, s2, s3) print(s)

Code language: PHP (php)

Output:

Python String Concatenation

Code language: JavaScript (javascript)

In this example, Python substitutes a %s in the literal string by corresponding string variable in the tuple that follows the % operator.

6) Concatenating strings using the format() method

You can use the format() method to concatenate multiple strings into a string. For example:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = '{} {} {}'.format(s1, s2, s3) print(s)

Code language: PHP (php)

Output:

Python String Concatenation

Code language: JavaScript (javascript)

In this example, you use the {} in the string literal and pass the string that you want to concatenate to the format() method. The format() method substitutes the {} by the corresponding string argument.

7) Concatenating strings using f-strings

Python 3.6 introduced the f-strings that allow you to format strings in a more concise and elegant way.

And you can use the f-strings to concatenate multiple strings into one. For example:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = f'{s1} {s2} {s3}' print(s)

Code language: PHP (php)

Output:

Python String Concatenation

Code language: JavaScript (javascript)

Which method should you use to concatenate strings

Even though there’re multiple ways to concatenate strings in Python, it’s recommended to use the join() method, the + operator, and f-strings to concatenate strings.

Did you find this tutorial helpful ?

Concatenation means joining strings together end-to-end to create a new string. To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.

Even though there're multiple ways to concatenate strings in Python, it's recommended to use the join() method, the + operator, and f-strings to concatenate strings.

+= operator You can append another string to a string with the in-place operator, += . The string on the right is concatenated after the string variable on the left. If you want to add a string to the end of a string variable, use the += operator.

Concatenating means obtaining a new string that contains both of the original strings. In Python, there are a few ways to concatenate or combine strings. The new string that is created is referred to as a string object. In order to merge two strings into a single object, you may use the + operator.