data structures in Python, mainly lists, tuples, dictionaries, collections.
The most basic data structure in Python is the sequence (sequence). Each element in the sequence is assigned an ordinal-that is, the element's position, which is also an index. The first index is 0, the second one is 1, and so on.
Python contains 6 built-in sequences, and other built-in sequence types are strings, Unicode strings, buffer objects, and Range objects.
Python also has a data structure called a container (container). A container is basically any object that contains other objects. Sequences, such as lists and tuples, and mappings, such as dictionaries, are two main types of containers. Each element in the sequence has its own number, and each element in the map has a name (also called a key). As for container types that are neither sequential nor mapped, the set is an example.
列表用[]表示,元组用(),字典用{key:values...},set{...}列表和元组属于序列,其中列表是可变序列,可以直接对其进行赋值操作,元组是不可变序列。所有序列类型都可以进行某些特定法操作。这些操作包括:索引(indexing)、分片(sliceing)、加(adding)、乘(multiplying)以及检查某个元素是否属于序列的成员(成员资格)。
The list method can be applied to a sequence of types, not just a string
List method:
List.append Append new object to the end of the list
List.count Counts the number of occurrences of an element in the list
List.extend appends multiple values of another sequence at the end of the list
A = [All-in-one,] b=[,4,5,6] A.extend (b)
List.index find the index position of the first occurrence of a value from the list
List.insert inserts the object into the list
A = [three-in-one] A.insert (2,4)
List.pop removes the last element in the list and returns the value of the element
Use the Pop method to implement a common data structure-stack, LIFO (LIFO)
List.remove Remove one of the list's worthy first matches
List.sort sorts the list in its original location, which means changing the original list instead of simply returning a sorted copy of the list
When you need a well-ordered list copy and keep the original list intact, it is wrong to use the following procedure:
x = [' A ', ' B ', ' C ', ' d ']
y = X.sort ()
Print Y
The return is None
Because the sort method modifies X and returns a null value, the last result is a sorted x and Y with a value of none. The correct way to do this is to first assign a copy to Y and then sort y:
y = x[:]
Y.sort ()
Call x[again:] Get a shard that contains all the elements of X, which is a very efficient way to copy an entire list, but simply assigning X to Y is useless, because doing so is to point X and Y to the same list.
Another way to get a list of sorted copies is to use the sorted function:
This function can actually be used for any sequence, but it always returns a list.
Tuples: Immutable sequences
In addition to creating tuples and accessing tuple elements, there are not many other operations, such as shards of tuples or tuples, just like a list of shards or lists.
string: Formatting
S% is called the conversion specifier (conversion specifier), and they mark where the conversion value needs to be inserted. s indicates that the value will be formatted as a string, and if it is not a string, it will be converted to a string using str
string format conversion type (see conversion table for details)
character methods: Many methods are inherited from the string module
Find can look up a substring in a longer string, which returns the leftmost index where the substring is located, and returns a 1 if not found
Join S is the inverse of the split method, which is used to add elements to the team, and the team element that needs to be added must be a string
seq = [' 1 ', ' 2 '] sep = ' + ' sep.join (seq) or '/'. Join (SEQ)
Replace returns all occurrences of a string that are the resulting string
' Hello C + + '. Replace (' C + + ', ' python ')
Split it's the inverse of join, used to split a string into a sequence
' A+b+c+d '. Split (' + ') [' A ', ' B ', ' C ', ' d ']
dictionary: There is no special order, but is stored in a specific key (key), key can be a number, a string or even a tuple
The basic behavior of a dictionary is similar to a sequence in many ways:
Len (d) Returns the number of items in D (key-value pairs)
D[k] Returns the value associated to the key K
D[k]=v Associates the value V to the key K
del d[k] deletes items with a key of K
Although dictionaries and lists are of the same nature, there are some important differences
Key type: The key of a dictionary is not necessarily an integer data (but it may be), or it may be an immutable type. such as floating-point (real), string or tuple
Auto-add: Even if the key doesn't exist in the dictionary at first, it can be assigned a value so that the dictionary creates a new entry.  
Membership: expression K in D looks for a key, not a value.
dictionary method:
Clear clears all entries in the dictionary, which is an in-place operation, so no return value, or return as none
Copy Returns a new dictionary with the same key value (this method is shallow copy, with Deepcopys for deep copy) the
items and Iteritems items method returns all dictionary entries as a list, each of which comes from (the key, the value). However, the items are returned with no special order.
Keys and Iterkeys return the keys in the dictionary as a list, and Iterkeys returns to the key iterator. The
Pop method is used to get the value corresponding to the given key, and then remove the key value pair from the dictionary
Popitem is similar to List.pop, which pops up the last element, except that Popitem pops up random entries because the dictionary does not The concept of ' The last element ' is
values return the values in the dictionary as a list, unlike the list of return keys, which can contain duplicate elements in the list.
+
Tags: "Basic python Tutorial" second Edition
Python Basic Learning notes-0