built-in data structure classification:
Numeric type
int, float, complex, bool
Sequence Object
String str
Lists List
Tuple (tuple)
Key-value pairs
Set Set
Dictionary Dict
Digital type
int, float, complex, bool are class, 1,5.0, 2+3j are examples of objects
Int:python3 int is a long integer, no size limit, limited by memory size
float: With integer and fractional parts, supported by decimal and scientific notation, only double-precision
Complex: There are real and imaginary parts, both real and imaginary parts are floating point numbers, 3+4j
Subclass of Bool:int, with only 2 instances true and false corresponding to 1 and 0, can be directly calculated with an integer
List ()
the list is mutable , continuous (sequence), linear data structures that can be indexed, data structures that can be iterated
Distinguish:
list: Find Fast ... But from the time of revision (Increase/deletion), it is very troublesome and slow
Link list: Find slow ... But the changes are fast and the lookup is slow
Queue (Queued): FIFO ~
Stack : Advanced post-out, LIFO (stack)
List definition: initialization
list ()->new empty list
list (iterable), new list initialized from iterable ' s items
list cannot be defined at the beginning of a size
LST = list () lst = []LST = [2,5,6, ' ab ']lst = List (range (5))
Indexed index: Also called subscript, starting from 0?
Positive index: From left to right, starting with 0, numbering each cell in the list
Negative index: From right to left, starting from-1
a positive or negative index cannot be bounded, or one occurs: Index Error
List access by index: List[index],index is the index, which includes access
List Query methods:
1.l.index (Valve,[start,stop])
Finds whether elements in a list match from a specified interval through element values
Match to first returns index immediately
No match, throw exception valveerror
2.count (Valve)
Returns the number of matches to valve in the list
Complexity of Time:
The index and Count methods are both O (n) [traversal]
As the list data size increases, the efficiency decreases
Len (): The length of the output list
List element Modification
Index access modification, index does not exceed bounds
List[index] = Valve
List added, insert list
Return none means that no new list is generated, in-place modification
1.l.append (object), None
2.l.insert (index,object), None
Inserts a language element at the index specified
Time complexity is O (n)
Here index can be hyper-bounded:
Beyond the upper bound, the tail is appended;
Beyond the Nether, head append
3.l.extend (iterable), None
Returns a list, which means that a new list is generated
1. + List
Connection operation, connecting two lists
Create a new list, the original list does not change
The __add___ () method is essentially called
2. * List
x = [[x[0][1]] * 3print (x)] = 20print (x) [[1, 2, 3], [1, 2, 3], [1, 2, 3]][[1,, 3], [1,, 3], [1,, 3]]y = [1] * 5y[0] = 6y[1] = 7print (y) [6, 7, 1, 1, 1]
List Delete Element
1. L.remove (valve), None
Finds the value of the first matching valve from left to right, removes the element, and returns none
In-place modification
Efficiency: Time complexity = O (n)
2. L.pop ([index]), item
An element is popped from the tail of the list without specifying index
Specifies index, which pops an element from index, which throws an indexerror error
Efficiency: Do not specify index time complexity = O (1), specify index (from the beginning, or the middle), time complexity =o (n)
3. L.clear ()-None
Other list operations
1. L.reserve ()- None
2. L.sort (key=none,reserve=flase), None
Sort list elements, in-place modification, default ascending
Reserve is true, reversed, descending
Key= a function to sort by the contents of a key
Lst.sort (Key=functionname), exp:lst.sort (key = str) sorted by string
3. In, No in
[3,4] in [1,2,[3,4]]
[5] not in [1,2,[3,4]]
For x in [1,2,[3,4]]
List replication
List l.copy ()
1. Shallow copy Shadow copy:
A shadow copy, also known as a shallow copy, encounters a reference type and just copies a reference.
Lst0 = [1,[2,3,4],5]LST5 = Lst0.copy () lst5[1][1] = 20print (lst5) print (LST0) [1, [2, 20, 4], 5][1, [2, 20, 4], 5]
2. Deep copy Deepcopy
The Copy module provides deepcopy
Import Copylst0 = [1,[2,3,4],5]LST5 = Copy.deepcopy (lst0) lst5[1][1] = 20lst5! = = Lst0print (lst5) print (LST0) [1, [2, 20, 4], 5][1, [2, 3, 4], 5]
Random Number Stochastic module
1. Item Random.randint (A, B)
2. Random.randrange ([Start],stop,[step]), item
Gets a random number in the collection that increments by the specified cardinality from the specified range, and the base default value is 1. Random.randrange (1,7,2)
3. Item Random.choice ()
Randomly extracts an element from an element of a non-empty sequence, Exp:random.choice (range (10)) randomly picks an integer from 0 to 9. Random.choice ([1,3,5,7])
4. Random.shuffle (list), none
5. Random.sample (population,k), list
Random.sample ([' A ', ' B ', ' C ', ' d '],2) random.sample ([' A ', ' B '], 2
Meta-group
A set of ordered elements
Use parentheses () to indicate
Tuples are immutable objects
The definition of a tuple is initialized
Defined:
Tuple (), empty tuple
Tuple (iterable), tuple initialized from iterable ' s items
t = tuple ()
t = ()
t = tuple (range (1,7,2)) iterates over an object
t = (1,) # The definition of an element tuple, note to have a comma
t = (1,) * 5
T = (6) *
Access to tuple elements
Support Index (subscript)
Tuples access by index
Tuple[index]: t[1]
Positive index: From left to right, starting from 0
Negative index: From right to left, starting from-1
Positive and negative indexes are not hyper-bounded. Otherwise, an index Error is raised
Tuple query
1.t.index (Valve,[start,stop])
Finds whether elements within a tuple match from a specified interval by value
Match to first returns index immediately
No match, throw exception valveerror
2.count (Valve)
The index and Count methods are both O (n) [traversal]
As the list data size increases, the efficiency decreases
Len (): Returns the number of elements
Tuples immutable, read-only, so no increment, delete, change method
Named Tuples Namedtuple
Namedtuple (typename,field_names,verbose= false,rename=false)
Name a tuple, return a subclass of a tuple, and define a field
Field_names can be a string of whitespace or comma-delimited fields, which can be a list of fields
From collections Import Namedtuplepoint = Namedtuple ("_point", ["X", "Y"]) # point for the returned class P = Point (11,22) Exp:form Collection s Import namedtuplestudent = Namedtuple ("Student", "Name Age") Tom = Student ("Tom") Jerry = Student ("jerry,18") Tome.name
Python built-in data structures-lists list, tuple tuple