This article brings you relevant knowledge about python, which mainly introduces issues related to lists, including accessing list elements, modifying, adding, deleting elements, organizing lists, etc. Let’s take a look at it together, I hope it will be helpful to everyone.
Recommended learning: python video tutorial
A list is composed of a series of elements in a specific order ! Lists are the most commonly used Python data type and can appear as a comma-separated value within square brackets. The data items of the list do not need to be of the same type! !
The list is an ordered set, so to access any element of the list, Just tell python the position (index) of that element. |
list = ['su liang','hacker','ice']print(list[0].title()) #结果:Su Liangprint(list[1].upper()) #结果:HACKERprint(list[2].lower()) #结果:ice
The elements returned by python here do not contain square brackets. Adding the title method can make the first letter capitalized. upper methodmakes all uppercase,lower methodmakes all lowercase! ! These methods can make the elements we access more concise! !
In python, the index of the first element of the list is 0, not 1. Most Programming languages are also stipulated in this way. It has been demonstrated for everyone in the above example. Python provides special syntax for accessing the last element. By specifying the index as -1, python can be accessed to the last element.
list = ['su liang','hacker','ice']print(list[-1]) #结果:iceprint(list[-2]) #结果:hacker
Most lists created are dynamic, meaning Then you can add, delete, modify and check the list. |
To modify list elements, you can specify the list name and the index of the element to be modified, Then specify the new value of the element.
list = ['su liang','hacker','ice']list[1]='hacker707'print(list)#结果:['su liang', 'hacker707', 'ice']
In many cases we need to continuously add new elements to the list. There are mainly the following methods.
The simplest way to add elements to a list is to append the append() method. Use this method to add elements to end of list.
x = []def list(name): global x x.append(name) print(x)while True: name = input('输入名字:') list(name)
Result:
Use the insert() method to add an index The sum value adds an element anywhere in the list.
list = ['su liang','hacker','ice']list.insert(1,'kiko')print(list)#结果:['su liang', 'kiko', 'hacker', 'ice']
In many cases we need to continuously delete some elements from the list. There are mainly the following methods.
If you know where the element to be deleted is in the list, you can use the del statement.
list = ['su liang', 'none', 'kiko', 'hacker', 'ice'] print(list.pop()) #结果:ice print(list) #结果:['su liang', 'none', 'kiko', 'hacker']
The pop() method deletes the element at the end of the list and allows you to continue using it.
list = ['su liang', 'none', 'kiko', 'hacker', 'ice'] print(list.pop()) #结果:ice print(list) #结果:['su liang', 'none', 'kiko', 'hacker']
In fact, you can use pop to delete any position in the list, the value needs to be indexed in brackets That’s it.
list = ['su liang', 'none', 'kiko', 'hacker', 'ice'] x = list.pop(3) print(x) #结果:hacker
Sometimes we don’t know where the element is in the list, but only the value of the element. You can use the remove() method to delete.
list = ['su liang', 'none', 'kiko', 'hacker', 'ice'] list.remove('none') print(list) #结果:['su liang', 'kiko', 'hacker', 'ice']
In the list you create, the order of the elements is unpredictable. Sometimes you need to preserve the original ordering of list elements, and sometimes you need to adjust the order. Python provides many ways to organize lists, which can be used according to the situation. |
在使用sort方法时,默认为从小到大,总a到z进行排序,依然可以在括号内加上reverse=True进行倒序.
此时的排序是对列表永久排序,即不保留原来的列表顺序!!!
list = ['su liang', 'none', 'kiko', 'hacker', 'ice'] list.sort() print(list) #结果:['hacker', 'ice', 'kiko', 'none', 'su liang'] list.sort(reverse=True) print(list) #结果:['su liang', 'none', 'kiko', 'ice', 'hacker']
sorted相对sort来说,它保留了原列表序列。若想倒序,添加reverse参数即可。
list = ['su liang', 'none', 'kiko', 'hacker', 'ice'] list2 = sorted(list) print(list2) #结果:['hacker', 'ice', 'kiko', 'none', 'su liang'] print(list) #结果:['su liang', 'none', 'kiko', 'hacker', 'ice']
要反转列表元素的排列顺序,可使用方法reverse().注意:这并不是将列表元素按顺序打印,而是将原列表元素进行反转。reverse方法也是永久改变列表顺序的,若想恢复,再对列表再次调用该方法即可。
list = [2,5,6,4,8,7] list.reverse() print(list) #结果:[7, 8, 4, 6, 5, 2]
使用len函数可快速获取列表的长度。
list = ['su liang', 'none', 'kiko', 'hacker', 'ice'] n = len(list) print(n) #结果:5
推荐学习:python视频教程
The above is the detailed content of Detailed explanation of python lists (summary sharing). For more information, please follow other related articles on the PHP Chinese website!