Sets (अनऑर्डर्ड डेटा, ऑप्टिमाइज़्ड लॉजिक ऑपरेशंस के साथ)

Python में डेटा टाइप्स

Jason Myers

Instructor

Set

  • Unique
  • Unordered
  • Mutable
  • गणित की Set Theory का Python इम्प्लिमेंटेशन
Python में डेटा टाइप्स

Sets बनाना

  • Sets किसी list से बनाए जाते हैं
cookies_eaten_today = ['chocolate chip', 'peanut butter', 
   ...: 'chocolate chip', 'oatmeal cream', 'chocolate chip']

types_of_cookies_eaten = set(cookies_eaten_today)
print(types_of_cookies_eaten)
set(['chocolate chip', 'oatmeal cream', 'peanut butter'])
Python में डेटा टाइप्स

Sets संशोधित करना

  • .add() एक-एक एलिमेंट जोड़ता है
types_of_cookies_eaten.add('biscotti')

types_of_cookies_eaten.add('chocolate chip')

print(types_of_cookies_eaten)
set(['chocolate chip', 'oatmeal cream', 'peanut butter', 'biscotti'])
Python में डेटा टाइप्स

Sets अपडेट करना

  • .update() किसी और set या list को मर्ज करता है
cookies_hugo_ate = ['chocolate chip', 'anzac']

types_of_cookies_eaten.update(cookies_hugo_ate)

print(types_of_cookies_eaten)
set(['chocolate chip', 'anzac', 'oatmeal cream',  'peanut butter', 'biscotti'])
Python में डेटा टाइप्स

Sets से डेटा हटाना

  • .discard() वैल्यू द्वारा एलिमेंट को सुरक्षित रूप से हटाता है
  • .pop() set से एक मनमाना एलिमेंट हटाकर लौटाता है (खाली होने पर KeyError)
types_of_cookies_eaten.discard('biscotti')

print(types_of_cookies_eaten)
set(['chocolate chip', 'anzac', 'oatmeal cream', 'peanut butter'])
types_of_cookies_eaten.pop()
types_of_cookies_eaten.pop()
'chocolate chip'
'anzac'
Python में डेटा टाइप्स

Set operations - समानताएँ

  • .union() सभी नामों का set लौटाता है (or)
  • .intersection() ओवरलैपिंग डेटा बताता है (and)
cookies_jason_ate = set(['chocolate chip', 'oatmeal cream',
'peanut butter'])
cookies_hugo_ate = set(['chocolate chip', 'anzac'])

cookies_jason_ate.union(cookies_hugo_ate)
set(['chocolate chip', 'anzac', 'oatmeal cream', 'peanut butter'])
cookies_jason_ate.intersection(cookies_hugo_ate)
set(['chocolate chip'])
Python में डेटा टाइप्स

Set operations - अंतर

  • .difference() उस set में मौजूद डेटा दिखाता है जिस पर मेथड चलाया गया, जो आर्ग्युमेंट्स में नहीं है (-)
  • टार्गेट महत्वपूर्ण है!
cookies_jason_ate.difference(cookies_hugo_ate)
set(['oatmeal cream', 'peanut butter'])
cookies_hugo_ate.difference(cookies_jason_ate)
set(['anzac'])
Python में डेटा टाइप्स

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

Python में डेटा टाइप्स

Preparing Video For Download...