Python12: Collection

Source: Internet
Author: User

Set is a set of elements to be weighed.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
Print (list_1)

Output:

{1, 2, 3, 4, 5, 6, 7, 8}

Process finished with exit code 0

Explain:

sets the list_1 for the collection, which is bad for printing.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_2 = Set ([2,6,0,66,22,8,4])
Print (list_1,list_2)
Print (List_1.intersection (list_2)) #print (List_1 & List_2)


Print (List_1.union (list_2)) #print (list_1 | list_2)


Print (List_1.difference (list_2)) # Print (list_1-list_2)

Output:

{1, 2, 3, 4, 5, 6, 7, 8} {0, 2, 66, 4, 6, 8, 22}

{8, 2, 4, 6}

{0, 1, 2, 3, 4, 5, 6, 7, 8, 66, 22}

{1, 3, 5, 7}

Process finished with exit code 0

Explanation:the equivalent operation is behind the "#" sign.

List_1.intersection (list_2): The intersection of two sets.

List_1.union (list_2): The set of two sets.

List_1.difference (list_2): The difference set of two sets.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_2 = Set ([2,6,0,66,22,8,4])
Print (List_1.issubset (list_2)) # Print (List_1 < list_2)

Print (List_1.issuperset (list_2)) # print (List_1 > List_2)

Output:

False

False

Process finished with exit code 0

Explanation:the equivalent operation is behind the "#" sign.

List_1.issubset (list_2): determines whether List_1 is a subset of List_2.

List_1.issuperset (list_2): Determines whether list_1 is The parent set of list_2.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_2 = Set ([2,6,0,66,22,8,4])
Print (List_1.symmetric_difference (list_2)) # print (list_1 ^ list_2)

Output:

{0, 1, 66, 7, 3, 22, 5}

Process finished with exit code 0

Explanation:the equivalent operation is behind the "#" sign.

List_1.symmetric_difference (list_2): The set of two sets, removing the intersection part.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_2 = Set ([2,6,0,66,22,8,4])
Print (List_2.isdisjoint (list_1))

Output:

False

Process finished with exit code 0

Explain:

List_2.isdisjoint (list_1): Returns True if two sets do not have an intersection .

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_1.add (123)
Print (list_1)

Output:

{1, 2, 3, 4, 5, 6, 7, 8, 123}

Process finished with exit code 0

Explain:

List_1.add (123): Adds a collection element.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_1.update ([12,23,34])
Print (list_1)

Output:

{1, 2, 3, 4, 5, 6, 7, 8, 34, 12, 23}

Process finished with exit code 0

Explain:

List_1.update ([12,23,34]): Also an element that adds a collection.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_1.remove (6)
Print (list_1)

Output:

{1, 2, 3, 4, 5, 7, 8}

Process finished with exit code 0

Explain:

List_1.remove (6): Removes the element from the collection.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_1.pop ()
Print (list_1)

Output:

{2, 3, 4, 5, 6, 7, 8}

Process finished with exit code 0

Explain:

List_1.pop (): Removes the element from the collection, starting with the default deletion.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
List_1.discard (123)
Print (list_1)

Output:

{1, 2, 3, 4, 5, 6, 7, 8}

Process finished with exit code 0

Explain:

List_1.discard (123): removes the element from the collection, unlike the Remove (), if there is no such element in the collection, the error is not, and remove will error.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
n = Len (list_1)
Print (n)

Output:

8

Process finished with exit code 0

Explain:

n = Len (list_1): The length of the collection.

Method Operation:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Mclind

List_1 = [1,2,3,4,5,6,6,7,8]
List_1 = Set (list_1)
Print (2 in list_1)

Output:

True

Process finished with exit code 0

Explain:

Print (2 in list_1): Determines whether it is in the collection.

Python12: Collection

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.