Python - The list (list) operation

1. Definitions

Keyword list: list

List [] enclosed by between data separated. Among the data list, it can be of any type. Values ​​can be repeated.

List elements is variable, the order was ordered.

E.g:

b = ["萝卜", "jason", "1234", True]

2. The list of query

Value:

When reading data list, the default indexes starting from 0, from -1 reverse start.

Syntax: list name [index]

E.g:

b = ["萝卜", "jason", "1234", True]
print(b[1])
print(b[-2])

result:
jason
1234

Take the index:

Getting indexed list elements directly to obtain the index value by the elements.

Syntax: list name .index (element values)

E.g:

# List Name .index (element value) 
B = [ " carrot " , " Jason " , 1234 , True]
 Print (b.index ( " Jason " ))

result:
1

3. Add a list

The new list is the last additional data from the list.

Syntax: list of variable names .append (value)

E.g:

b = ["萝卜", "jason", "1234", True]
b.append ( " Little Star " )
 Print (b)

result:
[ ' Radish ' , ' Jason ' , ' 1234 ' , True, ' small star ' ]

4. Modify the list

Modify the list of modifications need to specify the index, that the new assignment to the list of elements.

Syntax: list [index] = new value

E.g:

B = [ " carrot " , " Jason " , " 1234 " , True, " Little Star " ]
B [ . 3] = " Magic founder "     # to a corresponding position in the list, re-assignment. 
Print (b)

result:
[ ' Radish ' , ' Jason ' , ' 1234 ' , ' Magic founder ' , ' little stars ' ]

5. Delete list elements

Syntax: list of variable names .remove (value), del list variable name [index], a list of variable names .pop (index)

E.g:

# List of variable names .remove (value) 
b = [ " carrot " , " Jason " , " 1234 " , " Magic Patriarch " , " Little Star " ]
b.remove ( " Magic founder " )
 Print (B)

# Del list variable name [index] 
b = [ " carrot " , " Jason " , " 1234 " , " Magic Patriarch " , " Little Star " ]
 del b [3 ]
 Print (b)

# List of variable names .pop (index) 
b = [ " carrot " , " Jason " , " 1234 " , " Magic Patriarch " , " Little Star " ]
b.pop(3)
print(b)

result:
[ ' Radish ' , ' Jason ' , ' 1234 ' , ' little stars ' ]
[ ' Radish ' , ' Jason ' , ' 1234 ' , ' little stars ' ]
[ ' Radish ' , ' Jason ' , ' 1234 ' , ' little stars ' ]

6. Insert list element

Syntax: list of variable names .insert (index data)

E.g:

B = [ " carrot " , " Jason " , " 1234 " , " Magic founder " , " Little Star " ]
b.insert ( 2, " flying " )
 Print (B)

result:
[ ' Radish ' , ' Jason ' , ' flying ' , ' 1234 ' , ' Magic founder ' , ' little stars ' ]

7. sorted list

Use the list in ascending order: sort ()

Use the list in descending order: sort (reverse = True)

Use the list in reverse order: reverse ()

E.g:

# Ascending Sort () 
C = [89,12,44,5,863,455 ]
c.sort ()
print(c)

# Descending order Sort (Reverse = True) 
C = [89,12,44,5,863,455 ]
c.sort(reverse=True)
print(c)

# Reverse Reverse () 
C = [89,12,44,5,863,455 ]
c.reverse()
print(c)

result:
[5, 12, 44, 89, 455, 863]
[863, 455, 89, 44, 12, 5]
[455, 863, 5, 44, 12, 89]

8. List of merger

method one:

The combined list can directly use the + merge

List List A + B

E.g:

B = [ " carrot " , " Jason " , " 1234 " , " Magic founder " , " Little Star " ]
c = [89,12,44,5,863,455]
d = b + c
print(d)

result:
[ ' Radish ' , ' Jason ' , ' 1234 ' , ' Magic founder ' , ' little star ' , 89, 12, 44, 5, 863, 455]

Second way:

Extend through the contents of a list appended to the end of another list

Syntax: list of variable names A.extend (variable name list B)

E.g:

# List of variable names A.extend (variable name list B) 
b = [ " carrot " , " Jason " , " 1234 " , " Magic Patriarch " , " Little Star " ]
c = [89,12,44,5,863,455]
b.extend(c)
print(b)

result:
[ ' Radish ' , ' Jason ' , ' 1234 ' , ' Magic founder ' , ' little star ' , 89, 12, 44, 5, 863, 455]

9. Clear the entire list

Clear the entire list using chear

E.g:

B = [ " carrot " , " Jason " , " 1234 " , " Magic founder " , " Little Star " ]
b.clear()
print(b)

result:
[]

Guess you like

Origin www.cnblogs.com/renshengruxi/p/11928355.html