operaciones de string numpy | Función rjust()

numpy.core.defchararray.rjust(arr, width, fillchar=' ')es otra función para realizar operaciones de string en numpy. Devuelve una array con los elementos de arr justificados a la derecha en una string de longitud y ancho. Llena el espacio restante de cada elemento de la array usando el fillchrparámetro. Si fillchrno se pasa, llena los espacios restantes con espacios en blanco.

Parámetros:
arr : array_like de str o unicode.Input array.
ancho: El ancho final de cada string.
fillchar : El caracter para llenar el espacio restante.

Devuelve: [ndarray] Muestra una array justificada a la derecha de str o unicode, según el tipo de entrada.

Código #1:

# Python program explaining
# numpy.char.rjust() method 
  
# importing numpy 
import numpy as geek
  
# input array  
in_arr = geek.array(['Numpy', 'Python', 'Pandas'])
print ("Input array : ", in_arr) 
  
# setting the width of each string to 8
width = 8
  
# output array when fillchar is not passed
out_arr = geek.char.rjust(in_arr, width)
print ("Output right justified array: ", out_arr) 
Producción:

Input array :  ['Numpy' 'Python' 'Pandas']
Output right justified array:  ['   Numpy' '  Python' '  Pandas']

Código #2:

# Python program explaining
# numpy.char.rjust() method 
  
# importing numpy 
import numpy as geek
  
# input array  
in_arr = geek.array(['Numpy', 'Python', 'Pandas'])
print ("Input array : ", in_arr) 
  
# setting the width of each string to 8
width = 8
  
# output array 
out_arr = geek.char.rjust(in_arr, width, fillchar ='*')
print ("Output right justified array: ", out_arr) 
Producción:

Input array :  ['Numpy' 'Python' 'Pandas']
Output right justified array:  ['***Numpy' '**Python' '**Pandas']

Código #3:

# Python program explaining
# numpy.char.rjust() method 
  
# importing numpy 
import numpy as geek
  
# input array  
in_arr = geek.array(['1', '11', '111'])
print ("Input array : ", in_arr)
  
# setting the width of each string to 5
width = 5
  
# output array
out_arr = geek.char.rjust(in_arr, width, fillchar ='-')
print ("Output right justified array: ", out_arr) 
Producción:

Input array :  ['1' '11' '111']
Output right justified array:  ['----1' '---11' '--111']

Publicación traducida automáticamente

Artículo escrito por jana_sayantan 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 *