Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the collection of Python set

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report