Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet94/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   90   91   92   93   94   95   96   97   ...   453
uSINg dICTIoNARIES
Dictionaries take a different approach to storing information. Every other structure covered in this 
chapter uses some type of numeric index — a dictionary relies on a key. Using a key does increase
the memory footprint of a dictionary so you don’t want to use a dictionary all the time, even though the 
quick access of a key may seem quite attractive. The key does make it possible to access elements con-
siderably faster. Listing 4-13 shows a dictionary in use.
lISTINg 4-13: 
 Accessing data using a dictionary
# Create the dictionary.
PeopleColors = {‘George’:’Red’, ‘Ann’:’Purple’, ‘Sam’:’Yellow’}
# Randomly access a value.
lISTINg 4-12 
(continued)
548592c04.indd 74
2/24/10 12:47:38 PM
www.finebook.ir


Using Dictionaries
 

 
75
print ‘The color George likes best is’, PeopleColors[‘George’]
# Add a new person and color.
PeopleColors[‘Nancy’] = ‘Blue’
# Verify that the new person was added.
if PeopleColors.has_key(‘Nancy’):
print ‘Added Nancy with color’, PeopleColors[‘Nancy’]
# Iterate through all of the values. 
print ‘\nHere are the colors people like:’
for Key, Value in PeopleColors.iteritems():
print Key, ‘likes’, Value
# Pause after the debug session.
raw_input(‘Press any key to continue...’)
FIguRE 4-11: 
Use a deque when you need the extra left-side flexibility this structure can provide.
Creating a dictionary is different from other sorts of storage types. Notice that you enclose all of the 
values in curly braces ({}). The key appears first and the value second. You separate key from value 
using a colon (:). The key is normally a string, but the value can be anything. The example could just 
as easily have used numbers.
Accessing a particular dictionary element is different, too. You don’t need to know an index number — 
you simply need to know the value you want. In this case, the example shows the color that George 
likes best. The code doesn’t need to know that George appears first in the dictionary (he actually 
doesn’t, but more about that in a moment).
548592c04.indd 75
2/24/10 12:47:38 PM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   90   91   92   93   94   95   96   97   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish