Python data structure and lists

One, data structure

  • Data structure refers to the collection of data elements that have one or more relationships with each other and the relationship composition between the data elements in the collection
  • Simply put, the data structure is how the design data is organized and stored in the computer
  • Lists, collections, dictionaries, etc. are all a data structure
  • Program = data structure + algorithm

Second, the classification of data structure

The data structure can be divided into linear structure, tree structure, and graph structure according to its logical structure.

  • Linear structure: the elements in the data structure have a one-to-one relationship
  • Tree structure: The elements in the data structure have a one-to-many relationship
  • Graph structure: the elements in the data structure have a many-to-many relationship

Three, list

List is a basic data type. Python list is essentially an over-allocate array, such as the initialization list arr=[1,2,3,4], adding element 5 to it, and the expanded capacity of the underlying array corresponding to arr is not 5, but 8. The meaning of over-allocate is to allocate more storage space when expanding capacity. The advantage is to improve execution efficiency. When the number of elements stored in the list decreases, python will shrink the underlying array to avoid wasting memory.

Fourth, the time complexity analysis of each operation of the list

The list is not suitable for operations such as searching, deleting, and inserting elements, and the corresponding time complexity is O(n). Lists are more suitable for accessing elements of a certain index, adding elements to the end or deleting elements

Guess you like

Origin blog.csdn.net/zhengzaifeidelushang/article/details/112002545