Six Methods of integration in the Python dictionary

Source: Internet
Author: User

If you are confused about the actual application steps of the collection type in the Python dictionary, or are interested in the opposite, you can browse our articles, not only can you solve your problem, it can also stimulate your interest in the Python dictionary computer language.

Set Type

① Set () and frozenset () using the factory method of the set ():

  1. >>> s = set('cheeseshop')  
  2. >>> s  
  3. set(['c', 'e', 'h', 'o', 'p', 's'])  
  4. >>> t = frozenset('bookshop')  
  5. >>> t  
  6. frozenset(['b', 'h', 'k', 'o', 'p', 's'])  
  7. >>> type(s)  
  8. <type 'set'> 
  9. >>> type(t)  
  10. <type 'frozenset'> 

② How to update a set and use the built-in methods and operators of various sets to add and delete members of the Set:

  1. >>> s.add('z')  
  2. >>> s  
  3. set(['c', 'e', 'h', 'o', 'p', 's', 'z'])  
  4. >>> s.update('pypi')  
  5. >>> s  
  6. set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y', 'z'])  
  7. >>> s.remove('z')  
  8. >>> s  
  9. set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y'])  
  10. >>> s -= set('pypi')  
  11. >>> s  
  12. set(['c', 'e', 'h', 'o', 's'])  

③ Delete a set

  1. del s  

④ Member relationship (in, not in)

  1. >>> s = set('cheeseshop')  
  2. >>> t = frozenset('bookshop')  
  3. >>> 'k' in s  
  4. False  
  5. >>> 'k' in t  
  6. True  
  7. >>> 'c' not in t  
  8. True  

⑤ Set equivalence/non-equivalence

  1. >>> s == t  
  2. False  
  3. >>> s != t  
  4. True  
  5. >>> u = frozenset(s)  
  6. >>> s == u  
  7. True  
  8. >>> set('posh') == set('shop')  
  9. True  

⑥ Differential or relative complementary set (-) of the difference or relative complementary set of the Two sets (s and t) refers to a set C, the elements in the set, only belongs to the set s, not
In set t. The difference symbol has an equivalent method,

  1. difference().  
  2. >>> s - t  
  3. set(['c', 'e'])  

Symmetric Difference (^): symmetric difference is the XOR of the set. The above article describes the practical application steps of the Python dictionary for the set type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.