Detailed explanation of python lists (summary sharing)

WBOY
Release: 2022-05-12 21:23:02
forward
5212 people have browsed it

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.

Detailed explanation of python lists (summary sharing)

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! !

1. Access list elements

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
Copy after login

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! !

1.1 The index starts from 0 instead of 1

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
Copy after login

2. Modify, add and delete elements

Most lists created are dynamic, meaning Then you can add, delete, modify and check the list.

2.1 Modify list elements

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']
Copy after login

2.2 Adding elements to the list

In many cases we need to continuously add new elements to the list. There are mainly the following methods.

2.2.1 Add (append) at the end

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)
Copy after login

Result:
Detailed explanation of python lists (summary sharing)

2.2.2 Add (insert) at any position

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']
Copy after login

2.3 Delete elements from the list

In many cases we need to continuously delete some elements from the list. There are mainly the following methods.

2.3.1 Use the del statement to delete

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']
Copy after login
Copy after login

2.3.2 Use the pop() method to delete

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']
Copy after login
Copy after login

2.3.3 Pop the element at any position in the list

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
Copy after login

2.3.4 Remove elements based on value (remove)

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']
Copy after login

3. Organize the list

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.

3.1 使用sort()方法对列表永久排序

在使用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']
Copy after login

3.2 使用函数sorted()对列表临时排序

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']
Copy after login

3.3 倒着打印列表(reverse)

要反转列表元素的排列顺序,可使用方法reverse().注意:这并不是将列表元素按顺序打印,而是将原列表元素进行反转。reverse方法也是永久改变列表顺序的,若想恢复,再对列表再次调用该方法即可。

list = [2,5,6,4,8,7]
list.reverse()
print(list)
#结果:[7, 8, 4, 6, 5, 2]
Copy after login

3.4 确定列表长度(len)

使用len函数可快速获取列表的长度。

list = ['su liang', 'none', 'kiko', 'hacker', 'ice']
n = len(list)
print(n) #结果:5
Copy after login

推荐学习: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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!