1, Str.split (' delimiter ') returns a list based on the separator
Str.split (' delimiter ', n) returns a list of the split results of the first n delimiters
Note that Str cannot be none, otherwise it will report an exception
>>> a = "line1-abcdef \nline2-abc \nline4-abcd";
>>> a.split (' \ n ', 2)
[' Line1-abcdef ', ' line2-abc ', ' LINE4-ABCD ']
>>> a = "line1-abcdef \NLINE2-ABC \nline4-abcd \nhaha ";
>>> a.split (' \ n ', 2)
[' Line1-abcdef ', ' line2-abc ', ' LINE4-ABCD \nhaha ']
2. Conditional expression:
Two ways:
And-or expression::
((a > B) and [a] or [b]) [0]
If-else statement::
A if (a > B) Else b
3, sorted sorting
4, for the Loop pit
A = [1,2] for
item in a[0:1]:
Print Item
#结果: 1 for
item in A[0:2]:
Print Item
#结果: 1
2
The last element of this notation fragment is not included in the loop (Python version 2.7)
5, Python's dict,set,list,tuple simple analysis
https://my.oschina.net/courade/blog/143894
Dictionary (dict)
Dict surrounded by {}
Dict.keys (), Dict.values (), Dict.items ()
Hash (obj) returns the hashed value of obj if the return represents a key that can be used as a dict
Del or dict.pop can delete a item,clear erase all content
Sorted (dict) can dict sort
Dict.get () can find the non-existent key,dict.[]
Dict.setdefault () checks whether the dictionary contains a key. If this key exists in the dictionary, you can take it to its value. If the key you are looking for does not exist in the dictionary, you can assign a default value to the key and return this value.
{}.fromkeys () creates a dict, such as: {}.fromkeys (' love ', ' honor '), True) =>{' love ': true, ' honor ': true}
Do not allow a key to correspond to multiple values
The key value must be hashed, with the hash () test
An object that implements the hash () method to use as a key value
Collection (SET)
A collection is a mathematical concept that is created with set ()
Set.add (), set.update.set.remove, add update Delete, = can do set subtraction
Set.discard and Set.remove difference is that if the deleted element is not in the collection, discard does not complain, remove error
< <= representation subset,> >= representation superset
| Represents union & Representation intersection-representation difference set ^ difference set
Lists (list)
A list is a sequence object that can contain arbitrary Python data information, such as strings, numbers, lists, tuples, and so on. The data in the list is variable, we can add, modify, delete the data in the list through the object method. You can convert a sequence type into a list by using the list (SEQ) function.
Append (x) appends a single object x to the tail of the list. Using multiple parameters can cause an exception.
COUNT (x) returns the number of times the object x appears in the list.
Extend (l) adds the table items in list L to the list. Returns none.
Index (x) returns the indexes of the first list item in the list that matches the object X. An exception occurs when there is no matching element.
Insert (I,X) Inserts an object x before an element indexed to I. such as List.insert (0,X) Inserts an object before the first item. Returns none.
Pop (x) deletes the table entry in the list that is indexed as X and returns the value of the table entry. If no index is specified, the pop returns the last item in the list.
Remove (x) deletes the first element of the matching object x in the list. An exception was generated when the element was matched. Returns none.
Reverse () Reverses the order of the list elements.
Sort () Sorts the list, returning none. The Bisect module can be used to add and delete sorted list items.
META Group (tuple)
tuple= (1,), which is the tuple representation of a single element, requires an extra comma.
tuple=1,2,3,4, which can also be a tuple, allows Python to not use parentheses when it does not use parentheses without causing confusion.
As with lists, you can index, fragment, connect, and repeat tuples. You can also use Len () to find the tuple length.
The index of the tuple is in the form of tuple[i] instead of the tuple (i).
Similar to the list, use tuple (SEQ) to convert other sequence types to infinitesimal groups.
—————————————————— Split Line —————————————————————— –
The following excerpt from http://blog.csdn.net/gemini_dolphin/article/details/7786892
The performance gap between set and list in Python is hundreds of times times better to use set if you need to set and intersect (collections, lists, etc.).
The set is converted to list method as follows: List is converted to set method as follows:
s = set (' 12342212 ') L = [' 12342212 ']
Print S # set ([' 1 ', ' 3 ', ' 2 ', ' 4 ']) s = set (L[0])
L = List (s) print S # set ([' 1 ', ' 3 ', ' 2 ', ' 4 '])
L.sort () # Sort m = [' 11 ', ' 22 ', ' 33 ', ' 44 ', ' 11 ', ' 22 ']
Print L # [' 1 ', ' 2 ', ' 3 ', ' 4 '] print set (m) # set ([' 11 ', ' 33 ', ' 44 ', ' 22 '])
Visible set and Lsit are free to convert, and when you delete multiple/massive repeating elements in a list, you can convert to set and then back to list and sort (set without sorting). This method is not only convenient but also high efficiency.