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

What is the collection of Python basic data types

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

Share

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

In this issue, the editor will bring you about the collection of Python basic data types. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

As one of the basic data types of Python, collections are much more difficult than integers and strings. We need to have an in-depth understanding of collections and be proficient in using them.

1. So what is a collection?

Set is a basic data type of Python

A set is a mutable, unordered and unrepeatable sequence of elements

Collections cannot be accessed using indexes because collections (set) are unordered, but collections can be iterated

The element of the collection must be of type hash, that is, it must be hashable

To determine whether an object can be hash:hash ([1JZ 2])

two。 Can be hash type

Numerical type

Boolean type

String type

Bytes

Tuple

None

3. Hash type is not allowed

List

Bytearray

Set

4. Definition of set

# define empty set s = set () s = {} # Note: this is the # initialization of the definition dictionary. Initialize a collection s = {1Mague 2jin3} S1 = {(1mem2pce3), 100rec'} # tuples can be hash. So it can be outputted as an element of the collection print (S1) #: {'abc', 100,1)} S2 = {[1Magne2JE3], (1LJON2), the list of 100} # is not hash, so the output will report an error print (S2) # error S3 = set ([1mem2prime3]) print (S3) # output: {1mem2min3) # output

The syntax for defining a collection is: s = set (), while direct s = {} is the syntax for defining an empty dictionary

The initialization of the collection can be assigned directly in brackets {}, similar to: s = {1pm 2pm 3}

A collection defined with {} is the final form of the collection, so there can be no non-hash elements

You can pass in a list by using the set () method and the update () method, because the python program automatically converts the list into a collection, so you will find that through set ([1jin2d3]), the output will become like {1mem2d3}, that is, the list will be merged into a collection.

5. Insertion of collection

Define a set ss = set () s.add (1) s.add (1) print (s) # output: {1} because the set can be deduplicated

The insertion method of the collection is the add () method

The elements in the collection are not repeatable because the collection has a deduplication function

6. Modification of the collection

# define a set ss = set () s.update ([1, 2, 3, 4, 5]) # update () can pass on the list and will automatically convert to the set print (s) output: {1, 2, 3, 4, 5}

The modification method of the collection is the update () method, which can modify the elements in the collection.

7. Query for collection

S = {1 for item in s 2 3 pm 4} for item in s: print (item) output: 1234

Collections are unordered and cannot be queried using index indexes, but can only be queried by iterations

8. Deletion of collections

S = {1pion2, 3, 4, remove 5} # remove method s.remove (1) print (s) # output: {2, 3, 4, discard, 5} # discard, s.discard (6) print (s) # output: {1, pop, 3, 4, 4, 5, pop, s.pop () print (s) # output: {2, 3, 5, clear, s.clear () print (s) # output: set ()

The remove:remove (value) method removes a value from the element directly. If the element does not exist, the remove method will report an error

Discard: the discard method is similar to the remove method, except that if the element does not exist, discard will not report an error

Pop: generally speaking, the pop () method in a collection deletes elements randomly and reports an exception if the collection is empty

Clear: clear all elements in the collection

9. Object manipulation of a collection

Union: you can use the union () method to join two sets, or you can use the | symbol to join two sets (multiple elements can use the update () method)

`

S1 = {1pm 2pm 3pm 4}

S2 = {3pm 4pm 5pm 6}

S3 = {1pm 5pm 7pm 8}

The union of two sets:

Sets = s1.union (S2)

Print (sets) # output: {1,2,3,4,5,6}

Multiple sets are merged:

Sets = S1 | S2 | S3

Print (sets) # output: {1,2,3,4,5,6,7,8}

* intersection * *: you can use the intersection () method to find the intersection of two sets (multiple sets can use the intersection_update () method)

S1 = {1pm 2pm 3pm 4}

S2 = {3pm 4pm 5pm 6}

S3 = {4pm 5pm 6pm 7}

The intersection of two sets:

Sets = s1.intersection (S2)

Print (sets) # output: {3,4}

The intersection of multiple sets:

S1.intersection_update (* (s2mems3))

Print (S1) # output: {4}

* difference set * *: two sets can be subtracted using the difference method, for example, s1.difference (S2) indicates elements that S1 has but S2 does not have, and vice versa (multiple sets use the difference_update () method)

S1 = {1pm 2pm 3pm 4}

S2 = {3pm 4pm 5pm 6}

S3 = {5pm 6pm 7pm 8}

The difference set of two sets:

Sets = s1.difference (S2)

Print (sets) # output: {1,2}

Multiple sets subtraction sets:

S1.difference_update (* (s2mems3))

Print (S1) # output: {1,2}

`

10. The efficiency of set query

The query time complexity of linear structure is O (n). With the increase of data elements, the query time increases linearly.

Set and dict in Python can be considered as nonlinear structures, so the time complexity of query is not O (n), but its storage structure is hash table (hash table). In the best case, the query complexity is O (1).

So the search time of the collection does not increase with the increase of the set of elements.

The above is the collection of Python basic data types shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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