Python provides a lot of data structures, but many of them are simple operations, slow
Time complexity See:https://wiki.python.org/moin/TimeComplexity
Can Traverse (for): list, string, determine whether an element exists (in): list, string, tuple Merge: list, string element number (len ()) element and (sum ())
String (String)
' Banana ' Features: Immutable list
List (lists)
A = [1,'ABC', [A, B, C]] features: List sequence operations: query: A[l:r] (return to the list of the interval) a[l:r:k] (return [L, R-1] Insert: A.append (b) (trailing insert value B) Delete: Del A[i] (delete the element labeled I or an interval) comparison: ( Compares in dictionary order, returns TRUE or False) a==B a<b Shard Operation: A[l:r]= B (Will [l,r-1] The interval sequence is replaced with the sequence B length does not have to be the same) ADD: List method: (Generally for lists, strings, etc.) a=list (); 1>a.append (x) add element x at the end2>a.cound (x) statistic element x number of occurrences3> a.extend (b) Add list B at the end, equivalent to a = a +b4>a.index (x) returns the subscript of the first element as x5>A.insert (Loc, x) add element x at Loc subscript6>A.pop (loc) removes the element labeled Loc, without the LOC default last7>a.remove (x) Delete the first element with the value x8>a.reverse () reverse List a9>a.sort () sorts list A in addition to sort (key, reverse) You can provide comparison functions for sorting and whether to reverse sort as>>>a = ['AA','b','CCC'] >>>a.sort (key = len, reverse =True) #按字符串长度比较 key as the object of comparison, default from small to large, if from large to small only reverse= True
Dict (dictionary)
DIC ={} dic= {' One':'Uno',' Both':'dos','three':'Tres'} Features: Each entry is a key-value pair, each key operation: query: dic[' One'] (returns the key value of the specified key) Dic.keys () (Returns a list of all the keys of DIC) Dic.values () (Returns a list of all key values of DIC) Dic.items () (Returns all key values of DIC List of (two-tuple representations) Insert: Dic[a]=B (A (b) key-value pair) Delete: Dic.pop (i,j) (delete the key value pair with I as subscript if there is no return J) dic.clear () (Empty dictionary) del di C[B] (Delete key-value pairs with B as key)
Tuple (tuple):
t = (1,) (distinguishes between a tuple rather than a number) = (1'a', [1,2 ]) features: Immutable operation: query: dic[l:r] (return L to R-1 tuples) Dic.index (b) (subscript with return value B) Usage: = B, A (a B Exchange Assignment) (12) < (23) ( Similar to dictionary order method comparison size)
Set (SET):
s = {1,} #区别出这是集合而非字典 s=Set('spam') s=Set(['a','b']) Features: No repeat operation: query: Ainchs (returns whether a is present in the S collection) s|T S.union (t) (return s and T-set) s&T S.intersection (t) (return s and T intersection) s-T S.difference (t) (returns a collection of elements that have T in s) Insert: S.add (a) (insert element A) Delete: S.pop () (delete random element) S.remove (a) (delete element a) s.clear () (empty set S)
Python Learning First Play: basic data types