Variable size वाले keyword arguments
अब चलिए variable size वाले keyword arguments पर बढ़ते हैं! आपका काम key_types() फंक्शन को परिभाषित करना है. इसे keyword arguments की मनचाही संख्या लेनी चाहिए और एक नया dictionary लौटाना चाहिए: keys वे unique object types होंगे जो key_types() फंक्शन को दिए गए arguments के हैं, और उनसे जुड़ी values lists होंगी. हर list में उन argument के नाम होंगे जिनका type उस key के रूप में परिभाषित type से मेल खाता है (उदाहरण: key_types(kwarg1='a', kwarg2='b', kwarg3=1) कॉल करने पर {<class 'int'>: ['kwarg3'], <class 'str'>: ['kwarg1', 'kwarg2']} मिलेगा).
किसी object का type जानने के लिए type() फंक्शन का उपयोग करें (उदाहरण: type(1) है int).
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में कोडिंग इंटरव्यू प्रश्नों का अभ्यास
अभ्यास निर्देश
- फंक्शन को keyword arguments की मनचाही संख्या के साथ परिभाषित करें.
- key-value जोड़ों पर iterate करें.
- किसी key से जुड़ी list को अपडेट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Define the function with an arbitrary number of arguments
def key_types(____):
dict_type = dict()
# Iterate over key value pairs
for key, value in kwargs.____:
# Update a list associated with a key
if ____(____) in ____:
dict_type[____].____
else:
dict_type[____] = [____]
return dict_type
res = key_types(a=1, b=2, c=(1, 2), d=3.1, e=4.2)
print(res)