"Python Programming" 011 – Python Set: Master the power of unique data structures

Python provides various built-in data structures for storing and manipulating data. Among them, one of the most useful and powerful data structures is Set. A set is an unordered collection of unique elements that can be used to perform various operations such as union, intersection, and difference on multiple sets.

1. Create a collection

You can create a collection by enclosing a comma-separated list of values ​​in curly braces {}, or by using the set() constructor. For example:

fruits = {'apple', 'banana', 'orange'}
vegetables = set(['carrot', 'broccoli', 'spinach'])

In this example, fruits and vegetables are both sets containing three elements.

Note that collections can only contain immutable objects such as strings, numbers, and tuples. Mutable objects such as lists and dictionaries cannot be added to collections.

2. Access elements in a collection

Because the collection is an unordered collection, its elements cannot be accessed using an index. However, you can use the in keyword to check whether an element exists in the collection. For example:

if 'apple' in fruits:
    print('存在')
else:
    print('不存在')

In this example, the code checksfruits whether the string exists in the collection'apple'. Since it exists, the output is "exists".

3. Modify the collection

Collections are mutable, meaning you can add or remove elements from the collection. To add elements to a collection, you can use the add() method. For example:

fruits.add('pear')

In this example, the add() method adds the string 'pear' to the fruits collection.

To remove an element from a collection, you can use the remove() method. For example:

fruits.remove('banana')

In this example, the remove() method removes the string from the fruits collection. 'banana'

If you try to delete an element that does not exist in the collection, Python will throwKeyError an exception. To avoid this, use the discard() method. If the element exists in the collection, it is removed; if the element does not exist, nothing is done. For example:

fruits.discard('banana')

In this example, if the string exists in the fruits collection, the method deletes it; otherwise No action is taken. 'banana'discard()

4. Collection operations

Sets can be used to perform various operations such as union, intersection, and difference on multiple sets. These operations can be performed using collection methods or operators.

4.1 Union

The union of two sets is a set containing all the elements in the two sets without duplicate elements. The union operation can be performed using the union() method or the | operator. For example:

fruits = {'apple', 'banana', 'orange'}
berries = {'blueberry', 'raspberry', 'strawberry'}

all_fruits = fruits.union(berries)
# 或者
all_fruits = fruits | berries

In this example, both the union() method and the | operator return a value containing fruits and berriesThe set of all elements in the set without duplication.

4.2 Intersection

The intersection of two sets is a set containing only elements that exist in both sets. Intersection operations can be performed using the intersection() method or the & operator. For example:

apples = {'red', 'green', 'yellow'}
all_fruits = {'apple', 'banana', 'orange', 'blueberry', 'raspberry', 'strawberry'}

common_fruits = apples.intersection(all_fruits)
# 或者
common_fruits = apples & all_fruits

In this example, both the intersection() method and the & operator return a value containing only apples and all_fruitsA collection of common elements in the set.

4.3 Difference set

The difference set of two sets is a set containing elements that are present in one set but not in the other set. The difference operation can be performed using the difference() method or the - operator. For example:

fruits = {'apple', 'banana', 'orange'}
all_fruits = {'apple', 'banana', 'orange', 'blueberry', 'raspberry', 'strawberry'}

other_fruits = all_fruits.difference(fruits)
# 或者
other_fruits = all_fruits - fruits

In this example, both the difference() method and the - operator return a value that only exists in the all_fruits collection A collection of elements that do not exist in the fruitsset.

4.4 Symmetric difference set

The symmetric difference set of two sets is a set containing elements that exist in one set or the other set, but not in both sets. Symmetric difference operations can be performed using the symmetric_difference() method or the ^ operator. For example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

symmetric_diff = set1.symmetric_difference(set2)
# 或者
symmetric_diff = set1 ^ set2

In this example, the symmetric_difference() method and the ^ operator both return a value that exists in set1 or A set of elements that are in a>set2, but do not exist in both sets at the same time.

Summarize

By using collections, you can efficiently store and manipulate unique collections of elements. Sets provide a wealth of operation methods, including union, intersection, difference, and symmetric difference operations, allowing you to easily process set data. Master the use of sets in Python, and you will be able to better handle data and write more concise and efficient code.

English link:Python Data Structure: Set

Guess you like

Origin blog.csdn.net/zclmoon/article/details/132152234