Concatenación de elementos de dos arrays NumPy de string

En este artículo, discutiremos cómo concatenar elemento-sabio dos arreglos de string 

Ejemplo :

Input :
A = ['Akash', 'Ayush', 'Diksha', 'Radhika']
B = ['Kumar', 'Sharma', 'Tewari', 'Pegowal']

Output :
A + B = [AkashKumar, AyushSharma, 'DikshTewari', 'RadhikaPegowal']

Usaremos el método numpy.char.add() .

Sintaxis: numpy.char.add(x1, x2)
Parámetros:

  • x1: primer arreglo a ser concatenado (concatenado al principio)
  • x2: segunda array a concatenar (concatenada al final)

Devoluciones: Array de strings o unicode

Ejemplo 1: array de strings con un solo elemento.

Python3

# importing library numpy as np
import numpy as np
 
# creating array as first_name
first_name = np.array(['Geeks'],
                      dtype = np.str)
print("Printing first name array:")
print(first_name)
 
# creating array as last name
last_name = np.array(['forGeeks'],
                     dtype = np.str)
print("Printing last name array:")
print(last_name)
 
# concat first_name and last_name
# into new array named as full_name
full_name = np.char.add(first_name, last_name)
print("\nPrinting concatenate array as full name:")
print(full_name)

 
 Producción : 

Printing first name array:
['Geeks']
Printing last name array:
['forGeeks']

Printing concatenate array as full name:
['GeeksforGeeks']

Ejemplo 2: array de strings con varios elementos.

Python3

# importing library numpy as np
import numpy as np
 
# creating array as first_name
first_name = np.array(['Akash', 'Ayush', 'Diksha',
                       'Radhika'], dtype = np.str)
print("Printing first name array:")
print(first_name)
 
# creating array as last name
last_name = np.array(['Kumar', 'Sharma', 'Tewari',
                      'Pegowal'], dtype = np.str)
print("Printing last name array:")
print(last_name)
 
# concat first_name and last_name
# into new array named as full_name
full_name = np.char.add(first_name, last_name)
print("\nPrinting concatenate array as full name:")
print(full_name)

 
 Producción : 

Printing first name array:
['Akash' 'Ayush' 'Diksha' 'Radhika']
Printing last name array:
['Kumar' 'Sharma' 'Tewari' 'Pegowal']

Printing concatenate array as full name:
['AkashKumar' 'AyushSharma' 'DikshaTewari' 'RadhikaPegowal']

Publicación traducida automáticamente

Artículo escrito por hupphurr y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *