Python notes
Chapter I list and Ganso
1. General sequence operation
All sequences can perform certain operations, including: index (Indexing), shard (slicing), add (adding), multiply (multiplying), and check whether an element belongs to a list member.
Iteration: Repeats some operations on each element of the sequence in turn.
index of the sequence: the element can be positioned in the list by its position, which is the index of the list, indexed with an element similar to list[0], and index 0 points to the first element. You can also use negative numbers to index elements, and when you use negative numbers to index an element, the last element in the list is represented by-1, such as List[-1], which is the last element of the list.
shard of sequence : The Shard operation of a sequence requires two indexes as a boundary, the element of the first index is contained within the Shard, and the element of the second index is not contained within the Shard. For example, to take the third-to fifth-digit element of the list, you can use List[2:5], and if the portion of the Shard contains the tail of the list, you can leave the second index empty. For example list[3:], similarly, for the element of the header of the list can be, such as list[:-5].
For a shard, there is also a step parameter, which can be implicitly set, in an implicit setting, with a step of 1. The Shard operation iterates through the elements in the sequence in that step. For example, List[0:5:2] is a two-time step in the Calendar list from the beginning to the fifth element. Of course, the stride length can also be negative, and a negative number indicates a right-to-left traversal.
List Additions: Lists can be added to a larger list list=list1+list2, but the limit is the same type of list to add. Although strings are also lists, lists and strings cannot be added directly.
multiplication of a list : Multiplying a number by a list will generate a new list that repeats n times the old sequence of Jiangyou. The list content is empty you can use None to indicate that the list=[none]*10 represents a list that occupies 10 empty locations.
List Membership : You can use the In operator to check whether an element is a member of the list. The return value is a Boolean-type value that returns True if it is a member of the list, otherwise returns FLSE. For example ' a ' in list operation.
several built-in functions for the list : Len, Max, and Min are built-in functions in the list. The Len function returns the number of elements that the list contains. The Max and Min functions return the largest and smallest elements of the list, including Len (list), Max (list), min (list)
2. Operation of the list
Lists are also called "drudgery" in Python, and the list is mutable, with many built-in methods to manipulate the list.
List of strings : A list of strings can be used, such as list (' Hello '), which lists the string in characters.
List Basic Operations :
Assigning a value to a list element: list[3] = 5, assigning 5 to the fourth element in the list
Delete element in list: Del list[3], delete the fourth element in the list
Shard Assignment: You can replace a shard with a sequence of unequal lengths with the original sequence, and the Shard assignment can also insert a new element without replacing any of the original elements List[3:3] List1[2:3]=list2
List of methods :
1. Append: Appends a new object at the end of the list, such as List.append (4) to the end of the list.
2. Count: Counts the number of occurrences of an element in the list (only the elements of the list in the current layer), such as List.count (' a ')
3. Extend: You can append another list at the end of the list, similar to the list join operation, but the list join operation returns a new list, and extend returns the list after the original list is expanded, which is more efficient than the list join operation. For example List1.extend (LIST2)
4. Index: Find a value from the list the index position of the first occurrence, such as List.index (' a ')
5. Insert: Inserts the object at the specified position in the list, such as where List.insert (3, ' a ') is inserted in the position defined in the Insert parameter, and the value originally at that position will be moved backward one
6. Pop: Removes an element from the list (the last one by default) and returns the value of the element. List.pop (3) is to delete the value of the fourth position and return the value. There is no way to stack in Python, but you can use the Append and Pop methods to implement stack functions (FIFO, LIFO)
7. Remove: Removes the first occurrence of an element in the list, List.remove (' a ') removes the first matched a, but does not have a return value regardless of whether there is a match to the element.
8. Reverse: The elements in the list are stored in reverse, list.reverse (), and no return value.
9. Sort: Sort the list based on the original location, instead of returning a sorted copy. List.sort () The method also has no return value.
Sorted: This method sorts the source list and returns a new sorted copy of the list, which will not modify the order of the source list, list2=list1.sorted ()
11. Advanced sorting: If you want to define a collation, you can define a function, compare (x, y) returns a negative number when x<y, X>Y returns a positive number, X=Y returns a 0 defined function to sort the function as a sort parameter, and The sort also has two optional parameters, key and reverse, which, if required, are specified by the keyword parameter, and the value of key is the function used during the sorting process, such as List.sort (Key=len), sorted by the length of the element. The value of reverse is a value of type bool and, if true, reverses the sort, and the default is False for example List.sort (reverse=true) is the reverse sort.
3. Operation of the Yuan Zu
Ganso is also a sequence, the difference is that Ganso can not be modified, Ganso is created as a (three-way) special containing an element of the Ganso (1,) after the element is appended with a comma. In addition, Ganso does not have a list of methods.
Tuple function: This function converts a sequence to ganso, such as a tuple ([+/-)]
Ganso function: Can be used as a key in the mapping, and the list does not, Ganso as many of the intrinsic functions and methods of the return value exists.
This article is from the "Lemon" blog, be sure to keep this source http://xianglinhu.blog.51cto.com/5787032/1767111
Python notes _01 list and Ganso