अपने विशिष्ट प्रश्न के लिए, तुम सिर्फ इस का उपयोग कर सकते हैं:
>>> d={'fruit':
{'orange': 'orange', 'apple': 'red', 'banana': 'yellow'},
'vegetables':
{'lettuce': 'green', 'beet': 'red', 'pumpkin': 'orange'}}
>>> len(d)
2 # that is 1 reference for 'fruit' and 1 for 'vegetables'
>>> len(d['fruit'])
3 # 3 fruits listed...
>>> len(d['vegetables'])
3 # you thought of three of those...
>>> len(d['fruit'])+len(d['vegetables'])
6
आप विभिन्न उपकरण अजगर इस तुच्छ शब्दकोश में तत्वों की गिनती नहीं है का उपयोग कर सकते हैं, और अधिक रोचक और उत्पादक बात के बारे में सोचना है डेटा की संरचना पहली जगह में।
पायथन के मूल डेटा संरचना lists, sets, tuples, and dictionaries हैं। इन आंकड़ों में से कोई भी संदर्भ, संदर्भ के अनुसार, स्वयं के किसी भी नेस्टेड संस्करण या अन्य डेटा संरचनाओं को 'पकड़' सकता है।
इस सूची में एक nested list है:
>>> l = [1, [2, 3, [4]], [5, 6]]
>>> len(l)
3
>>> l[0]
1
>>> l[1]
[2, 3, [4]]
>>> l[2]
[5, 6]
पहला तत्व पूर्णांक 1. है तत्वों 1 और 2 सूचियों खुद को कर रहे हैं। यह मूल पायथन डेटा संरचनाओं के किसी अन्य के बारे में भी सच हो सकता है। ये recursive data structures हैं।
>>> color='color'
>>> family='family'
>>> sensation='sensation'
>>> good_things={
'fruit':
{
'orange':
{
color: 'orange',
family: 'citrus',
sensation: 'juicy'
},
'apple':
{
color: ['red','green','yellow'],
family:'Rosaceae',
'sensation': 'woody'
},
'banana':
{
color: ['yellow', 'green'],
family: 'musa',
sensation: 'sweet'
}
},
'vegatables':
{
'beets':
{
color: ['red', 'yellow'],
family: 'Chenopodiaceae',
sensation: 'sweet'
},
'broccoli':
{
color: 'green',
family: 'kale',
sensation: 'The butter you put on it',
}
}
}
अब जब कि डेटा के खिलाफ प्रश्नों अधिक मतलब:: आप उन pprint
साथ मुद्रित कर सकते हैं आप अपने शब्दकोश थोड़ा बेहतर ढंग से व्यवस्थित है, यह पायथन के सबसे सरल उपकरणों के साथ यह से जानकारी निकालने के आसान है
>>> len(good_things)
2 # 2 groups: fruits and vegetables
>>> len(good_things['fruit'])
3 # three fruits cataloged
>>> len(good_things['vegetables'])
2 # I can only think of two vegetables...
>>> print good_things['fruit']['apple']
{'color': ['red', 'green', 'yellow'], 'sensation': 'woody', 'family': 'Rosaceae'}
>>> len(good_things['fruit']['apple']['color'])
3 # apples have 3 colors
क्या 'LEN (food_colors [ 'फल']) + लेन (food_colors [ 'सब्जियों'])' के साथ गलत क्या है? –