সমস্যা বিবরণ
NumPy অ্যারে প্রসেসিংয়ের সময় নির্দিষ্ট রেঞ্জের মধ্যে পড়া উপাদানগুলো ফিল্টার করতে হয়। NumPy-এর ইনবিল্ট মেথডগুলো দিয়ে এটা করা যায়।
পদ্ধতি ১: np.logical_and() এবং np.where() ব্যবহার
np.where(np.logical_and(A >= 6, A <= 15)) দিয়ে ইনডেক্স পাওয়া যায়।
import numpy as np
A = np.array([5, 9, 11, 4, 31, 27, 8])
res = np.where(np.logical_and(A >= 6, A <= 15))
print("Array with condition (indices):", res) # (array([1, 2, 6]),)
পদ্ধতি ২: * অপারেটর ব্যবহার (বুলিয়ান মাস্ক)
A[(A >= 6) * (A <= 15)] দিয়ে সরাসরি ভ্যালু পাওয়া যায়।
import numpy as np
A = np.array([5, 9, 11, 4, 31, 27, 8])
res = A[(A >= 6) * (A <= 15)]
print("Array with condition (values):", res) # [9 11 8]