ডেটা অ্যানালিসিসের সময় ডিকশনারি থেকে নেগেটিভ ভ্যালুवाले কি-ভ্যালু পেয়ার রিমুভ করতে হয়। দুটি পদ্ধতি: ফর লুপ এবং ল্যাম্বডা ফাংশন।
১. ফর লুপ ব্যবহার
dict_1 = {'x':10, 'y':20, 'z':-30, 'p':-0.5, 'q':50}
print("Given Dictionary:", str(dict_1))
final_res_1 = dict((i, j) for i, j in dict_1.items() if j >= 0)
print("After filtering negative values:", str(final_res_1))
আউটপুট
Given Dictionary: {'x': 10, 'y': 20, 'z': -30, 'p': -0.5, 'q': 50}
After filtering negative values from dictionary: {'x': 10, 'y': 20, 'q': 50}
২. ল্যাম্বডা ফাংশন ব্যবহার
dictA = {'x':-4/2, 'y':15, 'z':-7.5, 'p':-9, 'q':17.2}
print("\nGiven Dictionary:", dictA)
final_res = dict(filter(lambda k: k[1] >= 0.0, dictA.items()))
print("After filtering negative values:", str(final_res))
আউটপুট
Given Dictionary: {'x': -2.0, 'y': 15, 'z': -7.5, 'p': -9, 'q': 17.2}
After filtering negative values from dictionary: {'y': 15, 'q': 17.2}