Write a NumPy program to add two zeros to the beginning of each element of a given array of string values
- برمجة بايثون
- 2021-09-13
- mhanasmh00489829403
الأجوبة
import numpy as np
nums = np.array(['1.12', '2.23', '3.71', '4.23', '5.11'], dtype=np.str)
print("Original array:")
print(nums)
print("\nAdd two zeros to the beginning of each element of the said array:")
print(np.char.add('00', nums))
print("\nAlternate method:")
print(np.char.rjust(nums, 6, fillchar='0'))
Sample Output:
Original array: ['1.12' '2.23' '3.71' '4.23' '5.11'] Add two zeros to the beginning of each element of the said array: ['001.12' '002.23' '003.71' '004.23' '005.11'] Alternate method: ['001.12' '002.23' '003.71' '004.23' '005.11']
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال