Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the collection of Python set

Shulou Source: shulou.com Published: 2022-06-01 16:06:09 09月21日 Update

This article mainly explains "how to use the collection of set of Python". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the collection of set of Python.

I. definition

The elements in the collection are unordered, unique, and immutable types.

A collection is a special list that can be deduplicated.

Lists = [1, set, 5, 7, 4, 6, 2, 7, 9] print (set (lists))

Use curly braces {} or the set () function to group the data together.

The parameters in set () can be tuples, strings, lists, or a collection. This parameter only needs to be a sequence.

You must use set () to create an empty collection, not curly braces. {} is used to define an empty dictionary.

II. Operation

Add (), which adds the elements to be passed in to the collection as a whole.

Update (), which splits the incoming element into a single character, stores it in the collection, and removes duplicate values.

Pop (), unordered collections, then delete the first element on the left side of the unordered collections, and report an error if the collection is empty.

Remove (), delete an element in the collection, and if the element does not exist, an error occurs.

Discard (), which also removes elements from the collection, and if the element does not exist, no error occurs.

Clear (), empty the collection.

Del (), clear the collection variable.

Set1 = {"Dili Reba", "Gurinaza", "Yi Yang Qianxi", "Ouyang Nana"} set1.add ("Jike Junyi") print (set1) set1.update ("Yang Wei Ling Hua") print (set1) print (set1.pop () set1.remove ("Ouyang Na") print (set1) set1.discard ("Gurinaza") print (set1) set1.clear () print (set1)

III. Operation

The operation in this part is similar to the algorithm of sets in mathematics.

Intersection: & or intersection

Union: | or union

Difference:-or difference

Subset:

Tags: Elements learning content parameters characters parentheses data errors Guli Ouyang operations special intersection functions individual variables subsets dictionaries strings that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Shulou Technology MySQL Shulou Information