Python basic in...login
Python basic introductory tutorial
author:php.cn  update time:2022-04-18 16:14:50

Python list(List)


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

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

Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.

List is the most commonly used Python data type, which can appear as a comma-separated value within square brackets.

The data items in the list do not need to be of the same type

To create a list, just use square brackets to enclose the different data items separated by commas. As shown below:

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

Like the index of a string, the list index starts from 0. Lists can be intercepted, combined, etc.


Access the values ​​in the list

Use subscript index to access the values ​​in the list. You can also use square brackets to intercept characters, as shown below:

#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

The above example output results:

list1[0]: physics
list2[1:5]: [2, 3, 4, 5]

Update list

You can modify or update the data items of the list, you can also use the append() method to add list items, as shown below:
#!/usr /bin/python

list = ['physics', 'chemistry', 1997, 2000];

print "Value available at index 2 : "
print list[2];
list[2] = 2001;
print "New value available at index 2 : "
print list[2];

Note: us The use of the append() method will be discussed in the next chapter

The above example output result:

Value available at index 2:
1997
New value available at index 2 :
2001

Delete list elements

You can use the del statement to delete elements of the list, as shown in the following example:

#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];

print list1;
del list1[2];
print "After deleting value at index 2 : "
print list1;

The above example output result:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

Note:We will discuss the use of remove() method in the next chapter


Python list script operators

The list pair + and * operators are similar to string operators. The + sign is used for combined lists, and the * sign is used for repeated lists.

looks like this:

Python expressionResultDescription
len([1, 2, 3])3Length
[1, 2, 3] + [4 , 5, 6][1, 2, 3, 4, 5, 6]combination
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repeat
3 in [ 1, 2, 3]TrueWhether the element exists in the list
for x in [1, 2, 3]: print x ,1 2 3Iteration

##Python list interception

Python list interception and characters String operation type, as follows:

L = ['spam', 'Spam', 'SPAM!']
Operation:

Python expressionResultDescriptionL[2]'SPAM !'Read the third element in the listL[-2]'Spam'Read The second to last element in the list L[1:]['Spam', 'SPAM!']from the second Elements start to intercept the list
##Python list functions & methods

Python contains the following functions:

Serial number1Compare the elements of two lists2Number of elements in the list##3max(list)4min(list)5 list(seq)

Python contains the following methods:

Function
cmp(list1, list2)
len(list)
Return the maximum value of the list element
Return the minimum value of the list element
Convert tuple to list
Serial numberMethod
1 list.append(obj)
Add a new object at the end of the list
2list.count(obj)
Count the number of elements in the list The number of occurrences in
3list.extend(seq)
Append multiple values ​​from another sequence at the end of the list at once (use a new list Expand the original list)
4list.index(obj)
Find the index position of the first matching item of a value from the list
5list.insert(index, obj)
Insert object into the list
6list.pop(obj=list[-1])
Remove an element in the list (the last element by default) and return the value of the element
7 list.remove(obj)
Remove the first occurrence of a value in the list
8list.reverse ()
Elements in the reverse list
9list.sort([func])
Sort the original list

php.cn