Python [3]-dictionary dic And set, python dictionary dicset

Source: Internet
Author: User
Tags union of sets

Python [3]-dictionary dic And set, python dictionary dicset
I. dictionary dict

Dict is stored as a key-value pair. The creation method is to use braces {} and separate the key and value with a colon.

>>> d={'chen':60,'zhang':80}>>> print d{'chen': 60, 'zhang': 80}

Assignment operation

>>> d['chen']=65>>> print d{'chen': 65, 'zhang': 80}

Value: get (). You can set the default value for the get () method.

>>> print d.get('liu',0)0

 

Delete: You can use pop to delete a value and return the deleted element.

d['liu']=85>>> d{'chen': 60, 'liu': 85, 'zhang': 80}>>> d.pop('liu')85>>> d{'chen': 60, 'zhang': 80}

The keys () and values () Methods return the dictionary's key and Value Sets respectively. Although there is no specific order for key-value pairs, the results returned by these two methods are in the same order.

>>> d.keys()['chen', 'zhang']>>> d.values()[60, 80]

Dictionary merge: You can use the update () method to merge two dictionaries.

>>> d1={'a':100,'b':99}>>> d.update(d1)>>> d{'chen': 60, 'a': 100, 'b': 99, 'zhang': 80}

 

Complete demo:

d={"chen":60,"zhang":80}print(d)d['chen']=65print(d)print(d.get("liu",0))d['liu']=85print(d)d.pop('liu')print(d)print(d.keys())print(d.values())d1={'a':100,'b':99}d.update(d1)print(d)

 

Ii. set

1. set is used to store a set of unique key values, but not their values.

2. set can be created in two ways:

  • Use the set function. The parameter is a list set, for example, s = set ([1, 2, 3, 4, 5]);
  • Set literal volume wrapped in braces, for example, s = {1, 3 }.

3. set common operations

  • The add (key) method adds an element to the set. It does not take effect if the element already exists.
  • The remove (key) method can delete elements.
  • | Operators can take the Union of sets.
  • & Operator returns the intersection of Sets
  • -Calculate the difference between the two sets.
  • ^ Returns the exclusive or of two sets.
>>> s=set([1,2,3,4,5])>>> print sset([1, 2, 3, 4, 5])>>> s.add(6)>>> print sset([1, 2, 3, 4, 5, 6])>>> s.add(6)>>> print sset([1, 2, 3, 4, 5, 6])>>> s.remove(6)>>> print sset([1, 2, 3, 4, 5])>>> s1=set([3,5,7])>>> print s&s1set([3, 5])>>> print s|s1set([1, 2, 3, 4, 5, 7])>>> print s1-sset([7])>>> print s^s1set([1, 2, 4, 7])

 

  • Issubset (): determines whether a set is a subset of another set;
  • Issuperset (): determines whether a set is a superset of another set.
>>> sset([1, 2, 3, 4, 5])>>> {1,3,5}.issubset(s)True>>> s.issuperset({1,5})True

 

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: info-contact@alibabacloud.com 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.