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

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

off999 2024-10-16 11:25 28 浏览 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']

列表方法

带列表的内置函数

相关推荐

戴尔官网保修查询入口(戴尔售后保质期查询)

可以按照以下步骤查询戴尔笔记本电脑的保修期:1.打开戴尔官网:https://www.戴尔.com/zh-cn/售后服务/保修政策.html2.点击页面上方的“服务与支持”按钮,进入戴尔的服务支持...

手机号邮箱登录入口(手机号邮箱官网)

手机163邮箱登录入口如下:163邮箱官网入口:https://smart.mail.163.com/login.htm点击进入登录或者注册邮箱即可。手机浏览器访问进入官网http://www.123...

sd卡(sd卡无法读取怎么修复)

  SD卡是大卡,相机用的;普通的手机内存卡,是小卡,正规的名称是macrosd卡,也就是微型SD卡。可以通过卡套转为普通的SD卡的大小。  其实就是大小不同。但手机上的内存卡,人们经常也俗称为SD...

路由器连接图(网络路由器连接图)
  • 路由器连接图(网络路由器连接图)
  • 路由器连接图(网络路由器连接图)
  • 路由器连接图(网络路由器连接图)
  • 路由器连接图(网络路由器连接图)
windows7蓝牙功能在哪里打开

点击搜索框在windows7系统主界面点击开始菜单,点击打开搜索框。输入命令输入services.msc后回车,在列表中找到并右击BluetoothSupportS...点击属性选择进入属性菜单,...

2010激活密钥(microsoft2010激活密钥)
2010激活密钥(microsoft2010激活密钥)

步骤/方式1officeprofessionalplus2010:(office专业版)6QFdx-pYH2G-ppYFd-C7RJM-BBKQ8Bdd3G-xM7FB-Bd2HM-YK63V-VQFdKVYBBJ-TRJpB-QFQ...

2025-11-19 04:03 off999

联想官方刷新bios工具(联想电脑刷新bios)

刷新BIOS需要使用联想的官方网站或授权维修中心来进行操作。以下是一些基本步骤:1.访问联想的官方网站,找到BIOS更新程序并下载。在下载过程中,请确保选择与您计算机型号匹配的版本。2.将下载的B...

苹果ios14系统下载(苹果ios14.1下载)
苹果ios14系统下载(苹果ios14.1下载)

1方法一步骤/方式一打开Appstore。步骤/方式二在搜索栏点击搜索框。步骤/方式三搜索并点击需要下载的软件。步骤/方式四点击获取。步骤/方式五最后验证ID密码即可。1.在应用商店搜索你要下载的应用名称。2.点击下载按钮,如果要求登...

2025-11-19 03:03 off999

office2010怎么免费永久激活密钥

用这个试试,一个KMS激活工具可以激活2010到2019的Office自家的目前用的就是这个microsoft6477.moe/1716.html直接使用这个Microsoftoffice2010...

类似爱加速的国内ip(类似爱加速的app)
类似爱加速的国内ip(类似爱加速的app)

推荐“V8盒子”。这一款免费无广告的模拟器,不同于其它软件盒子,而是类似于X8沙箱,满足游戏多开,画中画,悬浮球操作,熄屏后台运行等多功能的沙箱盒子.支持一键root,一键安装xposed框架,能在安卓/苹果手机上运行多个安卓/ios虚拟系...

2025-11-19 02:03 off999

阿里旺旺手机客户端(阿里旺旺手机app)

手机淘宝的旺旺在打开商品后,会看到左下角有个旺旺的图标,点击就可以联系了。  阿里旺旺是将原先的淘宝旺旺与阿里巴巴贸易通整合在一起的一个新品牌。它是淘宝和阿里巴巴为商人量身定做的免费网上商务沟通软件,...

最纯净的pe装机工具(pe工具哪个纯净)

U盘装系统步骤:1.制作U盘启动盘。这里推荐大白菜U盘启动盘制作工具,在网上一搜便是。2.U盘启动盘做好了,我们还需要一个GHOST文件,可以从网上下载一个ghost版的XP/WIN7/WIN8系统,...

装一个erp系统多少钱(wms仓库管理软件)

现在主流有客户端ERP和云端ERP两种客户端通常一次买断,价格在万元左右,但是还有隐性费用,你需要支付服务器、数据管理员,此外如果系统需要更新维护,你还需要支付另外一笔不菲的费用。云端ERP:优势...

cad2014序列号和密钥永久(autocad2014序列号和密钥)

1在cad2014中修改标注样式后,需要将其保存2单击“样式管理器”按钮,在弹出的窗口中选择修改后的标注样式,然后单击“设置为当前”按钮,再单击“保存当前样式”按钮,将其保存为新的样式名称3为了...

qq修改密保手机号(qq修改密保手机号是什么意思)

QQ更改绑定的手机号码操作步骤如下:1、打开手机主界面,找到“QQ”软件点击打开。2、输入正确的QQ账户和密码登录到qq主界面。3、点击左上角的头像“图片”,进入到个人中心界面。4、进入到个人中心界面...

取消回复欢迎 发表评论: