Python: lists, tuples

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43336822/article/details/102543255

List

1. Create a list of three kinds of ways:
1) Normal list: Number = [1,2,3,4,5], Member = [ 'sss',' WWW ', DSSA']
2) a mixed list: mix = [ . 1, 'SS', 3.14, [l, 2,3]]
. 3) empty list: empty = []

2. Add to the list of elements:
1) member.append ( 'mother') # You can only add an element
2) member.extend ([ 'I want to eat roasted yam', 'eat']) can add more than # element, but is a list of "[]" in braces
3) member.insert (2, 'chunks dropwise eat') # the first two methods can only be inserted in the tail, insert the insertion position can be specified, the first parameter represents position

3. Remove from list elements:
. 1): member.remove ( 'mom mom nice thank') # only need to know that this element is present in the list, you do not need the reference numeral
2): del member [1] # can also delete the entire list: del Member
. 3): member.pop () # default deletion without parameters queue tail element; delete return value is the value of the element, can be received by the variable
member.pop (1) # delete parameters the list corresponds to the subject of the next element

4. List fragment:
Member [. 1:. 3]: values in the list display elements 1 and 2 (the same is not header trailer)
shorthand:
Member [:. 3]: from zero by default, 0 is displayed, value of 2 element
member [1:]: defaults to the tail, the value is displayed 1,2,3,4 element (assuming captain. 5)
Member [:]: print a list of all the elements # member2 = member [:] receiving, obtain a copy of the original list, the list does not change the original impact of the new list, but list2 = llist1, until list1 modified, will change the contents of list2
Here Insert Picture Description
operator list 5:
comparison operators: <,>, =
connect operators: + (preferably extend)
repetition operators: * (replicated three times in the list of contents in the current list)
membership operator: in / not in # 'sarry ' in member, the return value is a boolean

6. The list of some common methods:
. 1) list.count (x): x is the number of elements in the list
2) list.index (x): x value of the element query index range tape # parameters can find the position of the element in the range: list.index ( 'sss', 2,. 7)
3) list.reverse (): this list is inverted, i.e., [1, 2, 3, 4] is inverted to [4, 3, 2, 1] # can not go to the brackets
4) list.sort (): Sort this list by default # row from small to large from small to large may want to sort and then reverse, sort of argument can also call: list. sort (reverse = True)

Tuple

Tuples can be seen asYou can not modify the list of elementsThe difference between the two is small, and mainly in the following list to illustrate the differences between tuples:

1. Create tuple: Use parentheses
Here Insert Picture Description
flag 2. tuple: ","
Here Insert Picture Description
PS Empty tuples: tmp = (); create a tuple element:. Tmp = (1,)

3. Update a tuple:
Here Insert Picture Description
because tuples can not be changed, so issliceTo create a new tuple to different modification operations

* 4. Remove the tuple:

	del tuple

ps: generally do not delete, because tuples can not change, do not change soon, and are using the new memory, so python collection mechanism to automatically reclaim him.

Guess you like

Origin blog.csdn.net/qq_43336822/article/details/102543255