Python basic core classic tutorial (021) - data structure public method

Copyright notice The author of this article: Gu Ge's younger brother Author's blog address: http://blog.csdn.net/lfdfhl...
Copyright notice
  • The author of this article: Gu Ge's younger brother
  • Author's blog address: http://blog.csdn.net/lfdfhl
summary

The so-called common method of data structure refers to the method that can be used by data structure such as list, tuple, dictionary, string, etc. Here, we summarize the common methods and operations of data structure in Python.

Built in functions

Here is a brief introduction to the common built-in functions included in Python

function describe remarks len(item) Count the number of elements in the container Also known as the number of data Del or del(item) Remove variables from container Two ways to pay attention to del max(item) Find maximum in container If it's a dictionary, only for key comparison min(item) Find minimum in container If it's a dictionary, only for key comparison

Example

""" //Original author: Gu Ge's younger brother //Blog address: http://blog.csdn.net/lfdfhl //Example description: data structure common method """ s1 = 'abcde' result = len(s1) print(result) s2 = 'abcde' del s2 # name 's2' is not defined # print(s2) l1 = [100, 200, 300, 400] del (l1[2]) print(l1) s3 = 'abcde' result = max(s3) print(result) result = min(s3) print(result) l2 = [100, 200, 300, 400] result = max(l2) print(result) result = min(l2) print(result)

operator operator Example result describe Supported data types + [9,5]+[2,7] [9, 5, 2, 7] merge String, list, tuple * "tom"*4 tomtomtomtom repeat String, list, tuple in 9 in (9, 5, 2,7) True Element exists String, list, tuple, dictionary not in 9 not in (4, 5, 6) True Element does not exist String, list, tuple, dictionary >Or > = or = = or < or<= (4, 5, 6) < (5, 5, 6) True Element comparison String, list, tuple

be careful:

  • in the operation of dictionary, the key of dictionary is judged
  • When not in operates a dictionary, it judges the key of the dictionary

Example

""" Original author: Gu Ge's younger brother Blog address: http://blog.csdn.net/lfdfhl Example description: data structure common method """ result = [9, 5] + [2, 7] print(result) result = "tom" * 4 print(result) result = (4, 5, 6) < (5, 5, 6) print(result)

Mutual transformation of data structure
  • tuple() converts other data structures into tuples
  • list() converts other data structures to lists
  • set() converts other data structures to sets

Example

""" //Original author: Gu Ge's younger brother //Blog address: http://blog.csdn.net/lfdfhl //Example description: mutual transformation of data structure """ l1 = [100, 200, 300, 400, 500] result = tuple(l1) print(result) s1 = {100, 200, 300, 400, 500} result = tuple(s1) print(result) t1 = ('a', 'b', 'c', 'd', 'e') result = list(t1) print(result) s2 = {100, 200, 300, 400, 500} result = list(s2) print(result) l2 = [100, 200, 300, 400, 500, 100, 200] result = set(l2) print(result) t2 = ('a', 'b', 'c', 'd', 'e') result = set(t2) print(result)


Summary

You can remove duplicate elements from a list by rotating the list into a collection.

section

Slicing refers to the operation of intercepting part of the original data. String, list and tuple all support slicing operation.

The syntax is as follows:

Original data [start position subscript: end position subscript: step size]

The main points are as follows:

  • 1. The section specified by the slice belongs to the left closed and right open type, i.e. including the beginning and not including the end.
  • 2. If you slice from the beginning, the start position subscript can be omitted, but the colon after it cannot be omitted
  • 3. If the slice ends at the end, the ending position subscript can be omitted, but the colons before and after it cannot be omitted
  • 4. The default step size is 1; if the slice is continuous, the colon and subsequent step size values can be omitted

Index order:

Python supports not only sequential index, but also reverse index, as shown below:

Sequential index is easy to understand and often used; so, what is reverse index? The so-called reverse index is to calculate the index from right to left. The rightmost index value is - 1, decreasing from right to left.

in other words:
The order index of characters in python is 0 1 2 3 4 5
The reverse index of python's characters is - 1 - 2 - 3 - 4 - 5 - 6

It can be seen from this that:
The index of p can be expressed as either 0 or - 6
The index of y can be expressed in either 1 or - 5
Other characters are similar; we will not repeat here.

Example

""" //Original author: Gu Ge's younger brother //Blog address: http://blog.csdn.net/lfdfhl //Example description: slice operation of data structure """ # Definition string number = "0123456789" # Intercepts a string from subscript 1 to subscript 3 result = number[1:4] print(result) # Intercept the string from the beginning to the end of subscript 4 result = number[4:] print(result) # Intercept string from start to subscript 6 result = number[:7] print(result) # Truncate full string result = number[:] print(result) # Intercept the string every other character from the start position result = number[::2] print(result) # From the position of subscript 1, every one character interval is intercepted result = number[1::2] print(result) # Intercept last string result = number[-1] print(result) # Intercepts the string from the subscript 2 position to the - 4 position result = number[2:-4] print(result) # Intercepts the last two characters of a string result = number[-2:] print(result) # Reverse string order result = number[::-1] print(result)

14 June 2020, 22:19 | Views: 3632

Add new comment

For adding a comment, please log in
or create account

0 comments