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 understand set in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about how to understand set in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Introduction

Set (collection) is rarely used, but it is an unordered sequence of non-repeating elements that can be easily removed and repeated quickly.

Definition of set

Set (collection) is similar to a list, except that the elements of the collection do not repeat

Collections, like dictionaries, are defined with {}, but elements are used, separated, or set ()

{} definition

#! / usr/bin/python3#-*-coding:utf-8-*-name_set = {'hui',' wang', 'zack',' hui'} print (name_set) # the result is {'hui',' wang', 'zack'} name_set = set (' hui', 'wang',' zack', 'hui')

Set () definition

Set () accepts only one parameter

In [13]: name_set = set ('hui') In [14]: name_setOut [14]: {' hui', 'wang','} In [15]: name_set = set (['hui',' wang', 'zack',' hui']) In [16]: name_setOut [16]: {'hui',' wang', 'zack'}

Note: empty collections cannot be defined with s = {}, which defaults to dictionaries and should be s = set ()

In [27]: s = {} In [28]: S1 = set () In [29]: type (s) Out [29]: dictIn [30]: type (S1) Out [30]: common operations of set collections

Collect all the built-in methods as follows:

There are too many methods. I'll choose a few commonly used tests.

Collection add elements

Use add () to add elements to the collection

In [33]: s = set () In [34]: s.add (1) In [35]: s.add (4) In [36]: s.add (3) In [37]: sOut [37]: {1,3,4} In [38]: s.add (2) In [39]: sOut [39]: {1,2,3,4} set remove elements

Remove () removes the element from the collection and reports an error if the element does not exist

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

Pop () randomly removes an element from the collection

In [38]: # remove () remove In [39]: sOut [39]: {1,2,3,4} In [40]: s.remove (3) In [41]: sOut [41]: {1,2 4} In [42]: s.remove (5)-KeyError Traceback (most recent call last) in-> 1 s .remove (5) KeyError: 5In [45]: # discard () remove In [46]: sOut [46]: {1 2,4} In [47]: s.discard (4) In [48]: sOut [48]: {1,2} In [49]: s.discard (3) In [50]: sOut [50]: {1,2} In [56]: # pop () Random remove In [57]: s.pop () Out [57]: 1In [58]: sOut [58]: {2,3,9 'hui'} In [59]: s.pop () Out [59]: 2In [60]: sOut [60]: {3,9,' hui'}

In fact, the pop method of the set collection deletes the first element on the left of the collection and returns the deleted element.

Collection statistics, emptying elements

Len () counts the number of set elements

Clear () clears the collection

In [68]: name_setOut [68]: {'wang',' zack'} In [69]: len (name_set) Out [69]: 2In [71]: name_set.clear () In [72]: len (name_set) Out [72]: 0In [73]: name_setOut [73]: set () Collection element acquisition (traversal)

The collection does not support indexing, and there is no way to fetch it, so it can only use for. In... Get the element by traversing.

In [81]: name_setOut [81]: {'hui',' wang' 'zack'} In [82]: for name in name_set:...: print (name)...: huiwangzackIn [83]: name_set [0] -TypeError Traceback (most recent call last) in-> 1 name_set [0] TypeError: 'set' object is not subscriptable set difference operation-In [89]: a = {1 2,3,4,5} In [90]: B = {1,2,3,6} In [91]: a-bOut [91]: {4,5} In [92]: B-aOut [92]: {6} In [93]: a.difference (b) Out [93]: {4,5} In [94]: b.difference (a) Out [94]: {6}

A-b is equivalent to dividing the same element b-a from a, that is, dividing the same element a-b from b is equivalent to a.difference (b).

Set union operation | In [95]: aOut [95]: {1,2,3,4,5} In [96]: bOut [96]: {1,2,3,6} In [97]: a | bOut [97]: {1,2,3,4,5,6} set intersection operation & In [99]: aOut [99]: {1,2,3,4,5} In 6} In: a & bOut: {1,2,3} set XOR operation ^ In: aOut: {1,2,3,4,5} In: bOut: {1,2,3,6} In

Remove all the same elements from the an and b sets, and the rest is the result of ^ XOR.

Application scenario ordinary for loop deduplication In [1]: li = [2,1,3,6,2,1] In [2]: temp = [] In [3]: for i in li:: if i not in temp:...: temp.append (I).: In [4]: liOut [4]: [2,1,3,6,2,1] In [5]: tempOut [5]: [2,1,3] 6] use the set to simply remove In: li = [1,2,2,3,3,5] In: li = set (li) In: liOut: {1,2,3,5} In: type (li) Out [109]: setIn [110]:

This changes the original list type into a collection type, which is even more difficult to operate, which is not the desired result.

So to keep the de-readd type unchanged, just nest another list ().

In: li = [1,2,2,3,3,5] In: li = list (set (li)) In: liOut: [1,2,3,5] In: type (li) Out: listIn

Use sort + set to remove weight

In [6]: list1 = [2,1,3,6,2,1] In [7]: list2 = list (set (list1)) In [8]: list2Out [8]: [1,2,3,6] In [9]: list2.sort (key=list1.index) In [10]: list2Out [10]: [2,1,3,6]

Use sorted + set to remove weight

In [12]: list1 = [2,1,3,6,2,1] In [13]: temp = sorted (set (list1), key=list1.index) In [14]: tempOut [14]: [2,1,3,6] after reading the above, do you have any further understanding of how to understand set in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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