Hash tables

Python में Data Structures और Algorithms

Miriam Antona

Software engineer

परिभाषा

  • आइटम्स का कलेक्शन स्टोर करता है
  • Key-value pairs

      lasagna: 14.75
      moussaka: 21.15
      sushi: 16.05
    
  • लगभग हर प्रोग्रामिंग भाषा में बिल्ट-इन hash table होता है:

    • hashes, hash maps, dictionaries, associative arrays
    • Python: dictionaries
Python में Data Structures और Algorithms

संरचना

  • हर पोजीशन: slot/bucket

खाली hash table का आरेखात्मक चित्रण.

Python में Data Structures और Algorithms

Hash functions

खाली hash table का आरेख. Lasagna की कीमत पाँचवें स्थान पर डाली गई है.

Python में Data Structures और Algorithms

Hash functions

Hash table का आरेख, बीच में "Hash function" लिखा है, जो "Lasagna" को पाँचवें स्थान वाले मान पर मैप करता है.

  • जब भी hash function लागू हो
    • same input पर same value लौटाए
Python में Data Structures और Algorithms

Lookups

कुछ मानों वाला hash table का आरेख.

  • "lasagna" खोजें
    • hash("lasagna") -> 5
Python में Data Structures और Algorithms

Lookups

कुछ मानों वाला hash table. पाँचवाँ स्थान हरा दिख रहा है.

  • "lasagna" खोजें
    • hash("lasagna") -> 5
    • 14,75 लौटाएँ
  • $O(1)$
Python में Data Structures और Algorithms

Collisions

  • Hash functions different inputs के लिए same output दे सकते हैं
  • hash("moussaka") -> 1

कुछ मानों वाला hash table. पहला स्थान पीला दिख रहा है.

  • insert: moussaka -> 21,15
  • Collision!
    • इसे resolve करना होगा
    • कई तकनीकें
Python में Data Structures और Algorithms

Python dictionary

  • खाली dictionary
my_empty_dictionary = {}
  • आइटम्स वाली dictionary
my_menu = {
    'lasagna': 14.75,
    'moussaka': 21.15,
    'sushi': 16.05
}
Python में Data Structures और Algorithms

Python dictionary - get

print(my_menu['sushi'])
16.05
print(my_menu['paella'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'paella'
print(my_menu.get('paella'))
None
Python में Data Structures और Algorithms

Python dictionary - items, keys & values

  • आइटम्स पाएं
print(my_menu.items())
dict_items([('lasanga', 14.75), 
            ('moussaka', 21.15),
            ('sushi', 16.05)])
  • keys पाएं
print(my_menu.keys())
dict_keys(['lasanga', 'moussaka', 'sushi'])
  • values पाएं
print(my_menu.values())
dict_values([14.75, 21.15, 16.05])
Python में Data Structures और Algorithms

Python dictionary - insert

my_menu['samosas'] = 13

print(my_menu.items())
dict_items([('lasanga', 14.75), ('moussaka', 21.15), ('sushi', 16.05), ('samosas', 13)])
Python में Data Structures और Algorithms

Python dictionary - modify

print(my_menu.get('sushi'))
16.05
my_menu['sushi'] = 20

print(my_menu.get('sushi'))
20
Python में Data Structures और Algorithms

Python dictionary - remove

  • dictionary को पूरी तरह हटाएँ
del my_menu
  • एक key-value pair हटाएँ
del my_menu["sushi"]
  • dictionary खाली करें
my_menu.clear()
Python में Data Structures और Algorithms

Python dictionary - iterate

for key, value in my_menu.items():
  print(f"\nkey: {key}")
  print(f"value: {value}")
key: lasagna
value: 14.75

key: moussaka
value: 21.15

key: sushi
value: 16.05
Python में Data Structures और Algorithms

Python dictionary - iterate

for dish in my_menu:
  print(dish)
lasagna
moussaka
sushi
for prices in my_menu.values():
  print(prices)
14.75
21.15
16.05
Python में Data Structures और Algorithms

Python dictionary - nested dictionaries

  • Nested dictionary
my_menu = {
  'sushi' : {
    'price' : 19.25,
    'best_served' : 'cold'
  },
  'paella' : {
    'price' : 15,
    'best_served' : 'hot'
  }
}
Python में Data Structures और Algorithms

अभ्यास करते हैं!

Python में Data Structures और Algorithms

Preparing Video For Download...