সমস্যা বিবরণ
পাইথন টাপল অর্ডার্ড এবং আনচেঞ্জেবল। কিন্তু এর উপাদানগুলো লিস্টও হতে পারে। একটি টাপল দেওয়া থাকলে সেটিতে কতগুলো লিস্ট আছে তা বের করতে হবে।
পদ্ধতি ১: len() ফাংশন ব্যবহার
len(tuple) সরাসরি টাপলের উপাদান সংখ্যা (যা লিস্ট) রিটার্ন করে।
tupA = (['a', 'b', 'x'], [21, 19])
tupB = (['n', 'm'], ['z', 'y', 'x'], [3, 7, 89])
print("The number of lists in tupA:", len(tupA)) # 2
print("The number of lists in tupB:", len(tupB)) # 3
পদ্ধতি ২: ইউজার-ডিফাইনড ফাংশন (UDF) ব্যবহার
বারবার ব্যবহার করলে ফাংশন বানানো ভালো। isinstance(tupl, tuple) চেক করুন, তারপর len()।
tupA = (['a', 'b', 'x'], [21, 19])
tupB = (['n', 'm'], ['z', 'y', 'x'], [3, 7, 89])
def getcount(tupl):
if isinstance(tupl, tuple):
return len(tupl)
else:
pass
print("The number of lists in tupA:", getcount(tupA)) # 2
print("The number of lists in tupB:", getcount(tupB)) # 3