Comparison summary of data containers for basic learning of python

17105641:

Data container classification

Data containers can be simply classified from the following perspectives:

① Whether subscript index is supported:
Supported: list, tuple, string - sequence type
Not supported: set, dictionary - non-sequence type

② Whether repeated elements are supported:
Supported: list, tuple, string - sequence type
not supported: Set, dictionary - non-sequence type

③ Whether it can be modified:
Supported: list, set, dictionary
Unsupported: tuple, string

Comparison of data container features

list tuple string gather dictionary
Number of elements Support multiple Support multiple Support multiple Support multiple Support multiple
element type arbitrary arbitrary characters only arbitrary Key: Value
Key: any type except dictionary
Value: any type
subscript index support support support not support not support
Repeating elements support support support not support not support
modifiability support not support not support support support
Data in order yes yes yes no no
scenes to be used Modifiable and repeatable batch data recording scenarios Unmodifiable and repeatable batch data recording scenarios Recording scene of a string of characters Non-repeatable data recording scenarios Data recording scenario of retrieving Value by Key

Application scenarios

Based on the characteristics of various types of data containers, their application scenarios are as follows:

List: A batch of data, a storage scenario that can be modified and repeatable.
Tuple: A batch of data, a storage scenario that cannot be modified and repeatable.
String: A storage scenario for a string of strings. Set: A batch of data, a dictionary
of deduplicated storage scenarios.
: A batch of data, storage scenarios where Key can be used to retrieve Value

Common operations for data containers

Common operations of data containers - traversal

Although data containers each have their own characteristics, they also have some common operations.

First, in terms of traversal:
①5 types of data containersBoth support for loopsTraverse
Lists, tuples, and strings support while loops, collections and dictionaries are not supported (cannot subscript index)

Although there are different forms of traversal, they all support traversal operations.

Example:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = []
i = 0
while i < len(my_list):
    if my_list[i] % 2 == 0:
        new_list.append(my_list[i])
    i += 1

print(f"偶数形成的新列表:{
      
      new_list}")

operation result:

New list formed from even numbers: [2, 4, 6, 8, 10]

General statistical functions of data containers

In addition to the commonality of traversal, data containers can share many functional methods.

len (container) : counts the number of
elements in the container ② max (container) : counts the largest element of the container
min (container) : counts the smallest element of the container

Example:

my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {
    
    1, 2, 3, 4, 5}
my_dict = {
    
    "key1": 1, "key2": 2, "key3": 3}

# len元素个数
print(f"列表 元素个数有:{
      
      len(my_list)}")
print(f"元组 元素个数有:{
      
      len(my_tuple)}")
print(f"字符串 元素个数有:{
      
      len(my_str)}")
print(f"集合 元素个数有:{
      
      len(my_set)}")
print(f"字典 元素个数有:{
      
      len(my_dict)}")

# max最大元素
print(f"列表 最大的元素是:{
      
      max(my_list)}")
print(f"元组 最大的元素是:{
      
      max(my_tuple)}")
print(f"字符串最大的元素是:{
      
      max(my_str)}")
print(f"集合 最大的元素是:{
      
      max(my_set)}")
print(f"字典 最大的元素是:{
      
      max(my_dict)}")

# min最小元素
print(f"列表 最小元素是:{
      
      min(my_list)}")
print(f"元组 最小元素是:{
      
      min(my_tuple)}")
print(f"字符串最小元素是:{
      
      min(my_str)}")
print(f"集合 最小元素是:{
      
      min(my_set)}")
print(f"字典 最小元素是:{
      
      min(my_dict)}")

operation result:

The number of elements in the list is: 5
The number of elements in the tuple is: 5
The number of elements in the string is: 7 The
number of elements in the collection is: 5
The number of elements in the dictionary is: 3
The largest element of the list is: 5
The largest element of the tuple is :5
The largest element of the string is: g
The largest element of the set is: 5
The largest element of the dictionary is: key3
The smallest element of the list is: 1 The smallest element
of the tuple is: 1
The smallest element of the string is: a
The smallest element of the set is: 1
The smallest element of the dictionary is: key1

Tips : Shortcut keys: shift+alt+drag the mouse: you can select multiple rows of the same position options at one time for replacement and other operations.

Universal conversion functions for containers

In addition to the commonality of subscript indexing, universal type conversion is also possible.

① list (container): Convert
to a list ② str (container): Convert
to a string ③ tuple (container): Convert the given container to a tuple
④ set (container): Convert the given container for collection

Example:

my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {
    
    1, 2, 3, 4, 5}
my_dict = {
    
    "key1": 1, "key2": 2, "key3": 3}
# 类型转换:容器转列表
print(f"列表转列表的结果是:{
      
      list(my_list)}")
print(f"元组转列表的结果是:{
      
      list(my_tuple)}")
print(f"字符串转列表结果是:{
      
      list(my_str)}")
print(f"集合转列表的结果是:{
      
      list(my_set)}")
print(f"字典转列表的结果是:{
      
      list(my_dict)}")
# 类型转换:容器转元组
print(f"列表转元组的结果是:{
      
      tuple(my_list)}")
print(f"元组转元组的结果是:{
      
      tuple(my_tuple)}")
print(f"字符串元组表结果是:{
      
      tuple(my_str)}")
print(f"集合转元组的结果是:{
      
      tuple(my_set)}")
print(f"字典转元组的结果是:{
      
      tuple(my_dict)}")
# 类型转换:容器转字符串
print(f"列表转字符串的结果是:{
      
      str(my_list)}")
print(f"元组转字符串的结果是:{
      
      str(my_tuple)}")
print(f"字符串字符串表结果是:{
      
      str(my_str)}")
print(f"集合转字符串的结果是:{
      
      str(my_set)}")
print(f"字典转字符串的结果是:{
      
      str(my_dict)}")
# 类型转换:容器转集合
print(f"列表转集合的结果是:{
      
      set(my_list)}")
print(f"元组转集合的结果是:{
      
      set(my_tuple)}")
print(f"字符串集合表结果是:{
      
      set(my_str)}")
print(f"集合转集合的结果是:{
      
      set(my_set)}")
print(f"字典转集合的结果是:{
      
      set(my_dict)}")

operation result:

The result of converting a list to a list is: [1, 2, 3, 4, 5]
The result of converting a tuple to a list is: [1, 2, 3, 4, 5]
The result of converting a string to a list is: ['a', ' b', 'c', 'd', 'e', ​​'f', 'g'] The
result of converting the set to a list is: [1, 2, 3, 4, 5]
The result of converting the dictionary to a list is: [' key1', 'key2', 'key3']
The result of converting list to tuple is: (1, 2, 3, 4, 5)
The result of converting tuple to tuple is: (1, 2, 3, 4, 5)
The result of string tuple table is: ('a', 'b', 'c', 'd', 'e', ​​'f', 'g') The result of set to tuple is: (1, 2
, 3, 4, 5)
The result of converting dictionary to tuple is: ('key1', 'key2', 'key3')
The result of converting list to string is: [1, 2, 3, 4, 5]
Tuple to character The result of string is: (1, 2, 3, 4, 5)
The result of string string table is:
The result of converting abcdefg set to string is: {1, 2, 3, 4, 5}
The result of converting dictionary to string It is: {'key1': 1, 'key2': 2, 'key3': 3}
The result of converting the list to the set is: {1, 2, 3, 4, 5}
The result of converting the tuple to the set is: {1, 2, 3, 4, 5}
The result of converting a string to a set is: {'d', 'f', 'b', 'e', ​​'g', 'c', 'a'} The result of converting a set to a set
is : {1, 2, 3, 4, 5}
The result of converting dictionary to set is: {'key3', 'key2', 'key1'}

Container universal sorting function

Syntax:
sorted(container, [reverse=True]): Sort the given container

Note that after sorting, you will get a list object, and the dictionary will lose its value .

Example:

# 进行容器的排序
my_list = [3, 5, 4, 1, 2]
my_tuple = (3, 5, 4, 1, 2)
my_str = "abcdefg"
my_set = {
    
    3, 5, 4, 1, 2}
my_dict = {
    
    "key1": 1, "key3": 3, "key2": 2}
# 正向排序
print(f"列表正向排序结果是:{
      
      sorted(my_list)}")
print(f"元组正向排序结果是:{
      
      sorted(my_tuple)}")
print(f"字符串正向排序结果是:{
      
      sorted(my_str)}")
print(f"集合正向排序结果是:{
      
      sorted(my_set)}")
print(f"字典正向排序结果是:{
      
      sorted(my_dict)}")
# 反向排序
print(f"列表反向排序结果是:{
      
      sorted(my_list, reverse=True)}")
print(f"元组反向排序结果是:{
      
      sorted(my_tuple, reverse=True)}")
print(f"字符串反向排序结果是:{
      
      sorted(my_str, reverse=True)}")
print(f"集合反向排序结果是:{
      
      sorted(my_set, reverse=True)}")
print(f"字典反向排序结果是:{
      
      sorted(my_dict, reverse=True)}")

operation result:

The result of the forward sorting of the list is: [1, 2, 3, 4, 5] The
result of the forward sorting of the tuple is: [1, 2, 3, 4, 5]
The result of the forward sorting of the string is: ['a', 'b', 'c', 'd', 'e', ​​'f', 'g'] The
result of the forward sorting of the set is: [1, 2, 3, 4, 5]
The result of the forward sorting of the dictionary is: [ 'key1', 'key2', 'key3']
The result of the reverse sorting of the list is: [5, 4, 3, 2, 1]
The result of the reverse sorting of the tuple is: [5, 4, 3, 2, 1]
characters The result of the reverse sorting of the string is: ['g', 'f', 'e', ​​'d', 'c', 'b', 'a'] The result of the reverse sorting of the set is: [5, 4, 3
, 2, 1]
The result of the reverse sorting of the dictionary is: ['key3', 'key2', 'key1']

Overview of common functions of containers

Function describe
Generic for loop Traverse the container (the dictionary is the traversal key)
max The largest element in the container
min() The smallest element in the container
len() Number of container elements
list() Convert to list
tuple() Convert to tuple
str() Convert to string
set() Convert to collection
sorted(sequence,[reverse=Ture]) Sorting, reverse=True means descending order to get a sorted list

Extension: String size comparison

ASCII code table:
Insert image description here

In the program, all characters used in strings such as:
① Uppercase and lowercase English words
② Numbers
③ Special symbols (!, \, |, @, #, spaces, etc.)
have their corresponding ASCII code table values.

Each character can correspond to the previous one: the code value of the number
String comparison is based on the size of the code value of the number.

String comparison

1. Strings are compared bit by bit , that is, one bit is compared.As long as one person is bigger, then the whole is bigger, and there is no need to compare later.
Example: abc is greater than ab, abd is greater than abc.

2. How to determine the size of individual characters?
Use the ASCII code table to determine the code value number corresponding to the character to determine the size.

Example:

# abc 比较abd
print(f"abd大于abc,结果:{
      
      'abd' > 'abc'}")
# a 比较ab
print(f"ab大于a,结果:{
      
      'ab' > 'a'}")
# a 比较A
print(f"a大于A,结果:{
      
      'a' > 'A'}")
# key1比较key2
print(f"key2大于key1,结果:{
      
      'key2' > 'key1'}")

operation result:

abd is greater than abc, result: True
ab is greater than a, result: True
a is greater than A, result: True
key2 is greater than key1, result: True

Guess you like

Origin blog.csdn.net/weixin_44996886/article/details/132678500