百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Python列表(List)(python列表list[0][1])

off999 2024-10-16 11:25 23 浏览 0 评论

列表(Lists)就像用其他语言声明的数组一样。列表不一定总是同质的,这使其成为Python中最强大的工具。单个列表可能包含数据类型,例如整数,字符串以及对象。列表是可变的,因此即使在创建后也可以对其进行更改。

Python中的List是有序的,并且有一个确定的计数。列表中的元素按照一定的顺序进行索引,并且列表的索引是在0为第一个索引的情况下完成的。列表中的每个元素在列表中都有其明确的位置,这允许复制列表中的元素,每个元素都有其独特的位置和可信度。

注意:列表是用于保留数据序列并对其进行进一步迭代的有用工具。

创建列表

Python中的列表可以通过将序列放在方括号[]中来创建。与集合不同,list不需要内置函数来创建list。

注意:与集合不同,列表可能包含可变元素。

# Python program to demonstrate  
# Creation of List  
  
# Creating a List 
List = [] 
print("Intial blank List: ") 
print(List) 
  
# Creating a List with  
# the use of a String 
List = ['GeeksForGeeks'] 
print("\nList with the use of String: ") 
print(List) 
  
# Creating a List with 
# the use of multiple values 
List = ["Geeks", "For", "Geeks"] 
print("\nList containing multiple values: ") 
print(List[0])  
print(List[2]) 
  
# Creating a Multi-Dimensional List 
# (By Nesting a list inside a List) 
List = [['Geeks', 'For'] , ['Geeks']] 
print("\nMulti-Dimensional List: ") 
print(List) 

输出:

Intial blank List: 
[]

List with the use of String: 
['GeeksForGeeks']

List containing multiple values: 
Geeks
Geeks

Multi-Dimensional List: 
[['Geeks', 'For'], ['Geeks']]

创建包含多个不同或重复元素的列表

列表可能包含具有不同位置的重复值,因此,在创建列表时,可以将多个不同或重复的值作为序列传递。

# Creating a List with  
# the use of Numbers 
# (Having duplicate values) 
List = [1, 2, 4, 4, 3, 3, 3, 6, 5] 
print("\nList with the use of Numbers: ") 
print(List) 
  
# Creating a List with  
# mixed type of values 
# (Having numbers and strings) 
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks'] 
print("\nList with the use of Mixed Values: ") 
print(List) 

输出:

List with the use of Numbers: 
[1, 2, 4, 4, 3, 3, 3, 6, 5]

List with the use of Mixed Values: 
[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']

向列表中添加元素

使用append()方法

可以使用内置的append()函数将元素添加到列表中。使用append()方法一次只能向列表中添加一个元素,如果使用append()方法添加多个元素,则使用循环。元组也可以使用append方法添加到列表中,因为元组是不可变的。与集合不同,还可以使用append()方法将列表添加到现有列表中。

# Python program to demonstrate  
# Addition of elements in a List 
  
# Creating a List 
List = [] 
print("Initial blank List: ") 
print(List) 
  
# Addition of Elements  
# in the List 
List.append(1) 
List.append(2) 
List.append(4) 
print("\nList after Addition of Three elements: ") 
print(List) 
  
# Adding elements to the List 
# using Iterator 
for i in range(1, 4): 
    List.append(i) 
print("\nList after Addition of elements from 1-3: ") 
print(List) 
  
# Adding Tuples to the List 
List.append((5, 6)) 
print("\nList after Addition of a Tuple: ") 
print(List) 
  
# Addition of List to a List 
List2 = ['For', 'Geeks'] 
List.append(List2) 
print("\nList after Addition of a List: ") 
print(List) 

输出:

Initial blank List: 
[]

List after Addition of Three elements: 
[1, 2, 4]

List after Addition of elements from 1-3: 
[1, 2, 4, 1, 2, 3]

List after Addition of a Tuple: 
[1, 2, 4, 1, 2, 3, (5, 6)]

List after Addition of a List: 
[1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']]

使用insert()方法

append()方法只适用于在列表末尾添加元素,对于在所需位置添加元素,则使用insert()方法。与只接受一个参数的append()不同,insert()方法需要两个参数(位置、值)。

# Python program to demonstrate  
# Addition of elements in a List 
   
# Creating a List 
List = [1,2,3,4] 
print("Initial List: ") 
print(List) 
  
# Addition of Element at  
# specific Position 
# (using Insert Method) 
List.insert(3, 12) 
List.insert(0, 'Geeks') 
print("\nList after performing Insert Operation: ") 
print(List) 

输出:

Initial List: 
[1, 2, 3, 4]

List after performing Insert Operation: 
['Geeks', 1, 2, 3, 12, 4]

使用extend()方法

除了append()和insert()方法之外,还有一个用于添加元素的方法extend(),该方法用于在列表末尾同时添加多个元素。

注意:append()和extend()方法只能在末尾添加元素。

# Python program to demonstrate  
# Addition of elements in a List 
    
# Creating a List 
List = [1,2,3,4] 
print("Initial List: ") 
print(List) 
  
# Addition of multiple elements 
# to the List at the end 
# (using Extend Method) 
List.extend([8, 'Geeks', 'Always']) 
print("\nList after performing Extend Operation: ") 
print(List) 

输出:

Initial List: 
[1, 2, 3, 4]

List after performing Extend Operation: 
[1, 2, 3, 4, 8, 'Geeks', 'Always']

访问列表中的元素

要访问列表项,请参考索引号。使用索引运算符[]访问列表中的项。索引必须是整数。使用嵌套索引访问嵌套列表。

# Python program to demonstrate  
# accessing of element from list 
  
# Creating a List with 
# the use of multiple values 
List = ["Geeks", "For", "Geeks"] 
  
# accessing a element from the  
# list using index number 
print("Accessing a element from the list") 
print(List[0])  
print(List[2]) 
  
# Creating a Multi-Dimensional List 
# (By Nesting a list inside a List) 
List = [['Geeks', 'For'] , ['Geeks']] 
  
# accessing a element from the  
# Multi-Dimensional List using 
# index number 
print("Acessing a element from a Multi-Dimensional list") 
print(List[0][1]) 
print(List[1][0]) 

输出:

Accessing a element from the list
Geeks
Geeks

Acessing a element from a Multi-Dimensional list
For
Geeks

负索引

在Python中,负序列索引表示数组末尾的位置。不必像List[len(List)-3]那样计算偏移量,只需编写List[-3]就足够了。负索引意味着从结尾开始,-1表示最后一项,-2表示第二个最后项等。

List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks'] 
  
# accessing a element using 
# negative indexing 
print("Acessing element using negative indexing") 
  
# print the last element of list 
print(List[-1]) 
  
# print the third last element of list  
print(List[-3]) 

输出:

Acessing element using negative indexing
Geeks
For

从列表中删除元素

使用remove()方法

可以使用内置的remove()函数从列表中删除元素,但是如果元素不存在于集合中,则会发生错误。Remove()方法一次只删除一个元素,要删除元素范围,则使用迭代器。remove()方法删除指定的项目。

注意:列表中的Remove方法将只删除搜索到的元素的第一个匹配项。

# Python program to demonstrate  
# Removal of elements in a List 
  
# Creating a List 
List = [1, 2, 3, 4, 5, 6,  
        7, 8, 9, 10, 11, 12] 
print("Intial List: ") 
print(List) 
  
# Removing elements from List 
# using Remove() method 
List.remove(5) 
List.remove(6) 
print("\nList after Removal of two elements: ") 
print(List) 
  
# Removing elements from List 
# using iterator method 
for i in range(1, 5): 
    List.remove(i) 
print("\nList after Removing a range of elements: ") 
print(List) 

输出:

Intial List: 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

List after Removal of two elements: 
[1, 2, 3, 4, 7, 8, 9, 10, 11, 12]

List after Removing a range of elements: 
[7, 8, 9, 10, 11, 12]

使用pop()方法

Pop()函数还可以用于从集合中移除和返回元素,但默认情况下,它只移除集合的最后一个元素。若要从列表的特定位置移除元素,元素的索引将作为参数传递给Pop()方法。

List = [1,2,3,4,5] 
  
# Removing element from the  
# Set using the pop() method 
List.pop() 
print("\nList after popping an element: ") 
print(List) 
  
# Removing element at a  
# specific location from the  
# Set using the pop() method 
List.pop(2) 
print("\nList after popping a specific element: ") 
print(List) 

输出:

List after popping an element: 
[1, 2, 3, 4]

List after popping a specific element: 
[1, 2, 4]

列表切片(Slicing of a List)

在Python List中,有多种方法可以打印包含所有元素的整个列表,但是要从列表中打印特定范围的元素,我们使用Slice操作。切片操作在使用冒号(:)的列表上执行。若要从开始到范围打印元素,请使用[:Index];若要从结束使用[:-Index]打印元素;若要从特定索引打印元素,直到结束使用[Index:];若要打印范围内的元素,请使用[开始索引:结束索引];若要使用切片操作打印整个列表,请使用[:]。此外,要按相反顺序打印整个列表,请使用[::-1]。

注意:要从后端打印列表元素,请使用负索引。

# Python program to demonstrate  
# Removal of elements in a List 
  
# Creating a List 
List = ['G','E','E','K','S','F', 
        'O','R','G','E','E','K','S'] 
print("Intial List: ") 
print(List) 
  
# Print elements of a range 
# using Slice operation 
Sliced_List = List[3:8] 
print("\nSlicing elements in a range 3-8: ") 
print(Sliced_List) 
  
# Print elements from a  
# pre-defined point to end 
Sliced_List = List[5:] 
print("\nElements sliced from 5th "
      "element till the end: ") 
print(Sliced_List) 
  
# Printing elements from 
# beginning till end 
Sliced_List = List[:] 
print("\nPrinting all elements using slice operation: ") 
print(Sliced_List) 

输出:

Intial List: 
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

Slicing elements in a range 3-8: 
['K', 'S', 'F', 'O', 'R']

Elements sliced from 5th element till the end: 
['F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

Printing all elements using slice operation: 
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

负索引列表切片

# Creating a List 
List = ['G','E','E','K','S','F', 
        'O','R','G','E','E','K','S'] 
print("Initial List: ") 
print(List) 
  
# Print elements from beginning 
# to a pre-defined point using Slice 
Sliced_List = List[:-6] 
print("\nElements sliced till 6th element from last: ") 
print(Sliced_List) 
  
# Print elements of a range 
# using negative index List slicing 
Sliced_List = List[-6:-1] 
print("\nElements sliced from index -6 to -1") 
print(Sliced_List) 
  
# Printing elements in reverse 
# using Slice operation 
Sliced_List = List[::-1] 
print("\nPrinting List in reverse: ") 
print(Sliced_List) 

输出:

Initial List: 
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

Elements sliced till 6th element from last: 
['G', 'E', 'E', 'K', 'S', 'F', 'O']

Elements sliced from index -6 to -1
['R', 'G', 'E', 'E', 'K']

Printing List in reverse: 
['S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G']

列表方法

带列表的内置函数

相关推荐

Alist 玩家请进:一键部署全新分支 Openlist,看看香不香!

Openlist(其前身是鼎鼎大名的Alist)是一款功能强大的开源文件列表程序。它能像“万能钥匙”一样,解锁并聚合你散落在各处的云盘资源——无论是阿里云盘、百度网盘、GoogleDrive还是...

白嫖SSL证书还自动续签?这个开源工具让我告别手动部署

你还在手动部署SSL证书?你是不是也遇到过这些问题:每3个月续一次Let'sEncrypt证书,忘了就翻车;手动配置Nginx,重启服务,搞一次SSL得花一下午;付费证书太贵,...

Docker Compose:让多容器应用一键起飞

CDockerCompose:让多容器应用一键起飞"曾经我也是一个手动启动容器的少年,直到我的膝盖中了一箭。"——某位忘记--link参数的运维工程师引言:容器化的烦恼与...

申请免费的SSL证书,到期一键续签

大家好,我是小悟。最近帮朋友配置网站HTTPS时发现,还有人对宝塔面板的SSL证书功能还不太熟悉。其实宝塔早就内置了免费的Let'sEncrypt证书申请和一键续签功能,操作简单到连新手都能...

飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx

前面分享了两期TVGate:Q大的转发代理工具TVGate升级了,操作更便捷,增加了新的功能跨平台内网转发神器TVGate部署与使用初体验现在项目已经开源,并支持Docker部署,本文介绍如何通...

Docker Compose 编排实战:一键部署多容器应用!

当项目变得越来越复杂,一个服务已经无法满足需求时,你可能需要同时部署数据库、后端服务、前端网页、缓存组件……这时,如果还一个一个手动dockerrun,简直是灾难这就是DockerCompo...

深度测评:Vue、React 一键部署的神器 PinMe

不知道大家有没有这种崩溃瞬间:领导突然要看项目Demo,客户临时要体验新功能,自己写的小案例想发朋友圈;找运维?排期?还要走工单;自己买服务器?域名、SSL、Nginx、防火墙;本地起服务?断电、关...

超简单!一键启动多容器,解锁 Docker Compose 极速编排秘籍

想要用最简单的方式在本地复刻一套完整的微服务环境?只需一个docker-compose.yml文件,你就能一键拉起N个容器,自动组网、挂载存储、环境隔离,全程无痛!下面这份终极指南,教你如何用...

日志文件转运工具Filebeat笔记_日志转发工具

一、概述与简介Filebeat是一个日志文件转运工具,在服务器上以轻量级代理的形式安装客户端后,Filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并将来自...

K8s 日志高效查看神器,提升运维效率10倍!

通常情况下,在部署了K8S服务之后,为了更好地监控服务的运行情况,都会接入对应的日志系统来进行检测和分析,比如常见的Filebeat+ElasticSearch+Kibana这一套组合...

如何给网站添加 https_如何给网站添加证书

一、简介相信大家都知道https是更加安全的,特别是一些网站,有https的网站更能够让用户信任访问接下来以我的个人网站五岁小孩为例子,带大家一起从0到1配置网站https本次配置的...

10个Linux文件内容查看命令的实用示例

Linux文件内容查看命令30个实用示例详细介绍了10个Linux文件内容查看命令的30个实用示例,涵盖了从基本文本查看、分页浏览到二进制文件分析的各个方面。掌握这些命令帮助您:高效查看各种文本文件内...

第13章 工程化实践_第13章 工程化实践课

13.1ESLint+Prettier代码规范统一代码风格配置//.eslintrc.jsmodule.exports={root:true,env:{node...

龙建股份:工程项目中标_龙建股份有限公司招聘网

404NotFoundnginx/1.6.1【公告简述】2016年9月8日公告,公司于2016年9月6日收到苏丹共和国(简称“北苏丹”)喀土穆州基础设施与运输部公路、桥梁和排水公司出具的中标通知书...

福田汽车:获得政府补助_福田 补贴

404NotFoundnginx/1.6.1【公告简述】2016年9月1日公告,自2016年8月17日至今,公司共收到产业发展补助、支持资金等与收益相关的政府补助4笔,共计5429.08万元(不含...

取消回复欢迎 发表评论: