"Seventh" in Python Learning: A collection in Python and the methods it has

Source: Internet
Author: User

1. Preface

The collection set in Python is similar to a list in that the biggest difference is that duplicate elements are not allowed in the collection and are automatically de-weighed if they contain duplicate elements when defined.
The collection is unordered, and the elements in the collection must be immutable types. The collection can be a key for the dictionary.

2. Definition of a collection

The collection is defined with a pair of curly braces {}

set = {'dog','cat','pig','monkey'}print(set)# 输出{'pig', 'cat', 'monkey', 'dog'}
3. Common operations for collections
    • Add (x)
      add element x to the end of the collection
set = {'dog','cat','pig','monkey'}set.add('china')set.add('china')print(set)# 输出{'cat', 'pig', 'china', 'monkey', 'dog'}

Note: Duplicate elements are not allowed inside the collection, and if duplicate elements are present, the collection is automatically de-weighed, so when you add two identical elements to the end of the collection, only one is preserved.

    • Clear ()
      Emptying elements within a collection
set = {'dog','cat','pig','monkey'}set.clear()print(set)# 输出set()
    • Pop ()
      Randomly deletes an element within the collection and returns the element that was deleted
set = {'dog','cat','pig','monkey'}print(set.pop())print(set)# 输出'pig'{'cat', 'monkey', 'dog'}
    • Remove (x)
      Delete the element x within the collection if the element x does not exist will be an error
set = {'dog','cat','pig','monkey'}set.remove('dog')print(set)set.remove('dog')# 输出{'pig', 'cat', 'monkey'}报错,因为已经把元素'dog'从集合中删去了,再次删除'dog'时,由于集合中已经不存在该元素了,所以报错
    • Discard (x)
      Delete element x within the collection, if element x does not exist will return none, no error
set = {'dog','cat','pig','monkey'}set.discard('dog')print(set)print(set.discard('dog'))# 输出{'pig', 'cat', 'monkey'}None
4. Relational operations of the set
    • Finding the intersection S1 & S2
set1 = {'a','b','c','d'}set2 = {'h','e','a','l','c'}print(set1 & set2)# 输出{'c', 'a'}
    • S1 Collection | S2
set1 = {'a','b','c','d'}set2 = {'h','e','a','l','c'}print(set1 | set2)# 输出{'c', 'a', 'h', 'd', 'b', 'l', 'e'}
    • Finding the difference set S1-S2
set1 = {'a','b','c','d'}set2 = {'h','e','a','l','c'}print(set1 - set2)# 输出{'b', 'd'}
5. Use the set to go back to the list

With the natural feature of not allowing duplicate elements within a set, we can use the

l1 = ['h','e','l','l','o']#1.将列表转为集合,集合会自动去重s1 = set(l1)#2.将去重后的集合再转回列表l1 = list(s1)print(l1)# 输出['l', 'o', 'h', 'e']

Presumably careful you have seen, go to the list of elements in the order changed, because the collection is unordered, ordered list into a set after the change into a disorderly, so the order of elements has changed, this is not what we want, we just want to remove the list of repeating elements and do not want to change the order of elements in the list, This is the drawback of this method of de-weight.

"Seventh" in Python Learning: A collection in Python and the methods it has

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: 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.