Write a NumPy program to count a given word in each row of a given array of string values

  • برمجة بايثون

Write a NumPy program to count a given word in each row of a given array of string values.

الأجوبة

import numpy as np 

str1 = np.array([['Python','NumPy','Exercises'],
                 ['Python','Pandas','Exercises'],
                 ['Python','Machine learning','Python']])
print("Original array of string values:") 
print(str1)
print("\nCount 'Python' row wise in the above array of string values:")
print(np.char.count(str1, 'Python'))

Sample Output:

Original array of string values:
[['Python' 'NumPy' 'Exercises']
 ['Python' 'Pandas' 'Exercises']
 ['Python' 'Machine learning' 'Python']]

Count 'Python' row wise in the above array of string values:
[[1 0 0]
 [1 0 0]
 [1 0 1]]
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...