List python3 basis of

Python3  list

Python sequence is the most basic data structure. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has a built-in type 6 sequences, but the most common are lists and tuples.

The sequence of operations can be carried out, including indexing, slicing, add, multiply, check members.

Further, Python already built determined sequence length and determining the maximum and minimum element method.

Python is a list of the most common types of data, it can appear as a comma-separated values ​​in square brackets.

Data items list need not have the same type

Create a list, as long as the comma-separated data items using different brackets can be. As follows:

list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

 The subscript index to access the values ​​in the list, you can also use the same form of brackets taken character, as follows:

#!/usr/bin/python3
 
list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
 
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

You can modify or update data items in the list, you can also add a list item using append () method, as follows:

! # / usr / bin / python3 
 
List = [ 'Google', 'Runoob', 1997, 2000] 
 
: Print (, List [2] "The third element is") 
List [2] = 2001 
post-print ( "Update the third element is: ", list [2])

You can use the del statement to delete the list of elements, the following examples:

! # / usr / bin / python3 
 
List = [ 'Google', 'Runoob', 1997, 2000] 
 
Print ( "the original list:", list) 
del List [2] 
Print ( "delete the third element:", list )

Similar lists of + and * operators string. + Number combination for the list, the list for repeatedly asterisk.

As follows:

len ([1, 2, 3]) # obtain a list of length 
[1, 2, 3] + [4, 5, 6] -> [1, 2, 3, 4, 5, 6] # list merge 
[ ' hi '] * 4 -> [ '!!!!! hi ',' hi ',' hi ',' hi '] # repeat string 
3 in [1, 2, 3 ] -> True # whether the element present in the list 
for x in [1, 2, 3]: print (x, end = "") -> 1 2 3 # iterations

List interception

L [0: 2] # intercept the first character to a third character 
L [-1] # reads the first element of the inverse of 
L [1:] # after acquiring all the elements from the second element

Python list of functions & methods

Get a list of elements number 
len (list)
Returns the list of elements the maximum value 
max (list)
Returns a list of the minimum element 
min (list) 
Converting the tuple list 
list (seq)
Add new object at the end of the list 
list.append (obj)
The number of times an element statistics appear in the list 
list.count (obj)
A plurality of values in another sequence added at the end of the list of one-time (original extended list with a new list) 
list.extend (SEQ) 

SEQ - element list may be a list of tuples, sets, dictionary, if a dictionary, only then will the key (key) as an element in turn added to the end of the original list. 
Examples 
#! / Usr / bin / python3 
 
List1 = [ 'Google', 'Runoob', 'Taobao'] 
list2 = List (the Range (5)) # create a list of 0-4 
list1.extend (list2) # expanded list 
print ( "the expanded list:", list1) 
a list of the extended: [ 'Google', 'Runoob ', 'Taobao', 0, 1, 2, 3, 4]
A value index to find the location of the first match from the list 
list.index (obj)  
The object is inserted into the list, index inserted before which the subscript 
list.insert (index, obj)
Removing one element in the list (default to the last element), and returns the value of the element 
list.pop ([index = -1])
Reverse arrangement of elements in the list	 
list.reverse ()
Clear List	 
list.clear () 
Copy the list	 
list.copy () 
Original list is sorted descending reverse = true indicates 
list.sort (key = None, reverse = False)
Remove the list a value of the first match	 
list.remove (obj)

  

  

  

  

  

 

  

  

  

  

  

  

  

 

Guess you like

Origin www.cnblogs.com/602681352jys/p/11445034.html