I. Data structure and access to help information
1. Data structure
A collection of data elements that are organized in a way, such as numbering elements , that can be numbers or characters, or even other data structures.
The most basic data structure of Python is the sequence
each element in the sequence is assigned an ordinal (that is, the position of the element), also known as an index : The index is numbered starting at 0
2. How to get command Help in Python
Gets the properties and methods used by the object:dir (),
Specific use of a method helps: Help()
Gets the document string for the callable object:print (obj.__doc__)
In [15]: dir (list) out[15]: [' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __dir__ ', ' __doc__ ', ', ' __eq__ ', ' __format__ ', ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __gt__ ', "__hash__ ', ' __iadd__ ', ' __imul__ ', ' __init__ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ', ' __len__ ', ' __lt__ ', ' __ mul__ ', ' __ne__ ', ' __new__ ', "__reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __ reversed__ ', ' __rmul__ ', ' __setattr__ ', "__setitem__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' append ', ' clear ', ' copy ', ' count ', ' extend ', ' index ', ' Insert ', ' Pop ', ' remove ', ' reverse ', ' sort '] in [17]: help (list) help on list object:class list (object) | list () -> new empty list | list (iterable) -> new list initialized from iterable ' s items | | methods defined here: | | __add__ (self, value, /) | return self+value. | | __contains__ (self, key, /) |   RETURN KEY IN SELF. |   |  __DELITEM__ (Self, key , /) | Delete self[key]. | | __eq__ (self, value, /) | return self==value. | | __ge__ (self, value, /) | return self>=value. in [20]: print (list.__doc__) list () -> new empty listlist (iterable) -> new list initialized from iterable ' s itemsin [21]: list.__doc__ out[21]: "list () -> new empty list\nlist (iterable) -> new list initialized from iterable ' S items '
Second, List
1. List
list : is an ordered collection of positions related to an arbitrary type of object.
A list is a sequence that is used to store data sequentially
The definition and initialization of a list:
In [5]: Lst1 = list () # using the Factory function list () in [6]: Lst2 = [] # using []in [7]: Type (LST1) out[7]: Listin [8]: type (LST2) OUT[8]: Listin [9]: Lst1 = List (range (10)) # Convert an iterative object to a list in [ten]: lst1out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
usually use the brackets when defining the list, and use the list () when converting an iteration object
Third, list-related operations
On the list generally have to increase, delete, change, check the relevant operation
1. Check
1) Access to the elements of the list by index (subscript)
returns the element corresponding to the index
index starting from the left, starting from 0, cannot go out of range, otherwise throws Indexerror
Negative index starts from the right, starting from 1
In []: lst1out[25]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]in []: lst1[0]out[26]: 0In [+]: lst1[10]--------------------------- ------------------------------------------------Indexerror Traceback (most recent ) <ipython-input-27-255d55760a91> in <module> ()----> 1 lst1[10]indexerror:list index out of Rangein [28]: LST1[-1]OUT[28]: 9In []: lst1[-3]out[29]: 7In [in]: lst1[-3]
2) List.index ()
returns the first index found to the element
If the element does not exist, the ValueError is thrown
The start parameter specifies the index from which to start the lookup; the stop parameter specifies which index to end from and does not contain the index
Start and stop can be negative, but always look from left to right
In [51]: help (Lst2.index) help on built-in function index:index (...) method of builtins.list instance l.index (value, [start, [Stop]] -> integer -- return first index of value. Raises valueerror if the value is not present. in [47]: lst2=[1, 3, 5, 2, 3, 5]in [48]: lst2.index (3) Out[48]: 1in [49]: lst2.index (2) Out[49]: 3in [52]: lst2.index (8)------------------------ ---------------------------------------------------valueerror Traceback (Most recent call last) < Ipython-input-52-857c8a1f260a> in&nBsp;<module> ()----> 1 lst2.index (8) Valueerror: 8 is not in listin [56]: lst2.index (3, 3) out[56]: 4in [57]: lst2.index (3, 3, 4)--------- ------------------------------------------------------------------valueerror Traceback (Most recent call last) <ipython-input-57-dd5e9d56cf7c> in <module> ()----> 1 lst2.index (3, 3, 4) Valueerror: 3 is not in listin [59]: lst2.index (3, 3, 5) Out[59]: 4in [60]: lst2.index (3, 4, 5) Out[60]: 4in [70]: lst2.index (3, -1, -6,) # start greater than stop is an empty list ------- --------------------------------------------------------------------valueerror Traceback (Most recent call last) < Ipython-input-70-b3ae59853639> in <module> ()----> 1 lst2.index (3, -1, -6,) Valueerror: 3 is not in listin [71]: lst2.index (3, -6, -1,) Out[71]: 1in [74]: lst2.index (3, -6, 9,) In [98]: lst2.index (3, 1, 1)---------------------------------------------------------------------------valueerror Traceback (most recent Call last) <ipython-input-98-a95f8fe9908B> in <module> ()----> 1 lst2.index (3, 1, 1) ValueError: 3 Is not in listin [99]: lst2.index (3, 1, 2) Out[99]: 1
The implementation of the List.index () function is prototyped:
def index (LST, value, start = 0, stop =-1): i = start for x in lst[start:end] if x = = Value:ret Urn I i + = 1 Rais valueerror ()
3) List.count ()
returns the number of occurrences of this value in the list
In [lst2out[89]: [1, 3, 5, 2, 3, 5]in [all]: Lst2.count (1) out[90]: 1In []: Lst2.count (5) out[91]: 2In []: Lst2.coun T (8) out[92]: 0
Prototype:
def count (lst, value): c = 0 for x in lst:if x = = Value:c + 1 return c
Summary:
The time complexity of index () and count () is O (n), also called linear complexity; efficiency is linearly correlated with data size
"Python" 06, Python built-in data structure 1