首頁 > 軟體

Python基礎學習--lesson9

2021-05-17 19:30:35
Python第九課的基礎知識
02:24來自LearningYard學苑

集合屬於Python無序可變序列,使用{}作為定界符,元素之間使用,分隔,同一個集合內的每個元素都是唯一的,元素之間不允許重複

集合中只能包含數字、字元串、元組等不可變類型的資料,而不能包含列表、字典、集合等可變類型的資料

The collection is a Python unordered variable sequence, using {} as the delimiter, use and separation between elements, each element in the same set is unique, and no repetition between elements is allowed.

The collection can only contain immutable types of data such as numbers, strings, tuples, and cannot contain variable types of data such as lists, dictionaries, and collections.

集合的創建與刪除

直接將集合賦值給變數即可創建一個集合物件

Assign the collection directly to the variable to create a collection object

也可以使用函數set()函數將列表、元組、字元串、range物件等其他可迭代物件轉換為集合,如果原來的資料中存在重複元素,則在轉換為集合的時候只保留一個;如果原序列或迭代物件中有不可的值,無法轉換成為集合,拋出異常

You can also use the set() function to convert other iterable objects such as lists, tuples, strings, and range objects into sets. If there are duplicate elements in the original data, only one will be kept when converting to a set; if the original There are unavailable values in the sequence or iteration object, which cannot be converted into a collection, and an exception is thrown

集合操作和運算

1. 集合元素增加與刪除

使用集合物件的add()方法可以增加新元素,如果該元素已存在則忽略該操作,不會拋出異常;update()方法用於合併另外一個集合中的元素到當前集合中,並自動去除重複元素。例如:

1. Add and delete collection elements

Use the add() method of the collection object to add new elements. If the element already exists, the operation will be ignored and no exception will be thrown; the update() method is used to merge elements from another collection into the current collection , And automatically remove duplicate elements. E.g:

2.pop()方法用於隨機刪除並返回集合中的一個元素,如果集合為空則拋出異常;

remove()方法用於刪除集合中的元素,如果指定元素不存在則拋出異常;

discard()用於從集合中刪除一個特定元素,如果元素不在集合中則忽略該操作;

clear()方法清空集合刪除所有元素

2. The pop() method is used to randomly delete and return an element in the collection, if the collection is empty, an exception will be thrown;

The remove() method is used to delete elements in the collection. If the specified element does not exist, an exception will be thrown;

discard() is used to delete a specific element from the collection, if the element is not in the collection, then ignore the operation;

The clear() method empties the collection and deletes all elements.

3.運算

例題:

可以使用集合快速提取序列中單一元素,即提取出序列中所有不重複元素

For example:

You can use the collection to quickly extract a single element in the sequence, that is, extract all unique elements in the sequence

例題:返回指定範圍內一定數量的不重複數字

Example: Return a certain number of unique numbers in the specified range

例題:下面兩段程式碼用來測試指定列表中是否包含非法資料,很明顯第二段使用集合的程式碼更高效一些。

Example: The following two pieces of code are used to test whether the specified list contains illegal data. Obviously, the second piece of code using sets is more efficient.

例題:假設已有若干使用者名字及其喜歡的電影清單,現有某使用者,已看過並喜歡一些電影,現在想找個新電影看看,又不知道看什麼好

Example: Suppose there are several user names and their favorite movies list. An existing user has watched and liked some movies, and now I want to find a new movie to watch, but I don’t know what to watch.

序列解包

可以使用序列解包功能對多個變數同時進行賦值

You can use the sequence unpacking function to assign values to multiple variables at the same time.

使用序列解包可以很方便地同時遍歷多個序列

Using sequence unpacking can easily traverse multiple sequences at the same time

對內建函數enumerate()返回的迭代物件進行遍歷

Traverse the iteration object returned by the built-in function enumerate( )

使用序列解包遍歷字典元素

Use sequence unpacking to traverse dictionary elements.

參考資料:谷歌翻譯

本文由LearningYard學苑原創,如有侵權,請聯絡刪除。


IT145.com E-mail:sddin#qq.com