Algorithms For Dummies


PART 2   Understanding the Need to Sort and Search



Download 7,18 Mb.
Pdf ko'rish
bet232/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   228   229   230   231   232   233   234   235   ...   651
Bog'liq
Algorithms

 

   


  PART 2 

 Understanding the Need to Sort and Search

Finding data using dictionaries

Creating and using a 

dictionary

 is much like working with a 

list

 except that you 



must now define a key and value pair. The great advantage of this data structure 

is that dictionaries can quickly provide access to specific data items using the key. 

There are limits to the kinds of keys you can use. Here are the special rules for 

creating a key:



 

»

The key must be unique. When you enter a duplicate key, the information 

found in the second entry wins; the first entry replaces the second.



 

»

The key must be immutable. This rule means that you can use strings

numbers, or tuples for the key. You can’t, however, use a 

list

 for a key.



The difference between mutable and immutable values is that immutable 

values can’t change. To change the value of a string, for example, Python 

actually creates a new string that contains the new value and gives the new 

string the same name as the old one. It then destroys the old string.

Python dictionaries are the software implementation of a data structure called a 

hash table, an array that maps keys to values. Chapter 7 explains hashes in detail 

and how using hashes can help dictionaries perform faster. You have no restric-

tions on the values you provide. A value can be any Python object, so you can use 

a dictionary to access an employee record or other complex data. The following 

example helps you understand how to use dictionaries better:

Colors = {"Sam": "Blue", "Amy": "Red", "Sarah": "Yellow"}

  

print(Colors["Sarah"])



print(Colors.keys())

  

for Item in Colors.keys():



   print("{0} likes the color {1}."

      .format(Item, Colors[Item]))

  

Colors["Sarah"] = "Purple"



Colors.update({"Harry": "Orange"})

del Colors["Sam"]

  

print(Colors)



  

Yellow


dict_keys(['Sarah', 'Amy', 'Sam'])

Sarah likes the color Yellow.

Amy likes the color Red.



CHAPTER 6


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   228   229   230   231   232   233   234   235   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
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