百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

Python列表(List) python列表list函数

bigegpt 2024-10-11 11:00 12 浏览

列表(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']

列表方法

带列表的内置函数

相关推荐

当Frida来“敲”门(frida是什么)

0x1渗透测试瓶颈目前,碰到越来越多的大客户都会将核心资产业务集中在统一的APP上,或者对自己比较重要的APP,如自己的主业务,办公APP进行加壳,流量加密,投入了很多精力在移动端的防护上。而现在挖...

服务端性能测试实战3-性能测试脚本开发

前言在前面的两篇文章中,我们分别介绍了性能测试的理论知识以及性能测试计划制定,本篇文章将重点介绍性能测试脚本开发。脚本开发将分为两个阶段:阶段一:了解各个接口的入参、出参,使用Python代码模拟前端...

Springboot整合Apache Ftpserver拓展功能及业务讲解(三)

今日分享每天分享技术实战干货,技术在于积累和收藏,希望可以帮助到您,同时也希望获得您的支持和关注。架构开源地址:https://gitee.com/msxyspringboot整合Ftpserver参...

Linux和Windows下:Python Crypto模块安装方式区别

一、Linux环境下:fromCrypto.SignatureimportPKCS1_v1_5如果导包报错:ImportError:Nomodulenamed'Crypt...

Python 3 加密简介(python des加密解密)

Python3的标准库中是没多少用来解决加密的,不过却有用于处理哈希的库。在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的软件包:PyCrypto和cryptography上,我...

怎样从零开始编译一个魔兽世界开源服务端Windows

第二章:编译和安装我是艾西,上期我们讲述到编译一个魔兽世界开源服务端环境准备,那么今天跟大家聊聊怎么编译和安装我们直接进入正题(上一章没有看到的小伙伴可以点我主页查看)编译服务端:在D盘新建一个文件夹...

附1-Conda部署安装及基本使用(conda安装教程)

Windows环境安装安装介质下载下载地址:https://www.anaconda.com/products/individual安装Anaconda安装时,选择自定义安装,选择自定义安装路径:配置...

如何配置全世界最小的 MySQL 服务器

配置全世界最小的MySQL服务器——如何在一块IntelEdison为控制板上安装一个MySQL服务器。介绍在我最近的一篇博文中,物联网,消息以及MySQL,我展示了如果Partic...

如何使用Github Action来自动化编译PolarDB-PG数据库

随着PolarDB在国产数据库领域荣膺桂冠并持续获得广泛认可,越来越多的学生和技术爱好者开始关注并涉足这款由阿里巴巴集团倾力打造且性能卓越的关系型云原生数据库。有很多同学想要上手尝试,却卡在了编译数据...

面向NDK开发者的Android 7.0变更(ndk android.mk)

订阅Google官方微信公众号:谷歌开发者。与谷歌一起创造未来!受Android平台其他改进的影响,为了方便加载本机代码,AndroidM和N中的动态链接器对编写整洁且跨平台兼容的本机...

信创改造--人大金仓(Kingbase)数据库安装、备份恢复的问题纪要

问题一:在安装KingbaseES时,安装用户对于安装路径需有“读”、“写”、“执行”的权限。在Linux系统中,需要以非root用户执行安装程序,且该用户要有标准的home目录,您可...

OpenSSH 安全漏洞,修补操作一手掌握

1.漏洞概述近日,国家信息安全漏洞库(CNNVD)收到关于OpenSSH安全漏洞(CNNVD-202407-017、CVE-2024-6387)情况的报送。攻击者可以利用该漏洞在无需认证的情况下,通...

Linux:lsof命令详解(linux lsof命令详解)

介绍欢迎来到这篇博客。在这篇博客中,我们将学习Unix/Linux系统上的lsof命令行工具。命令行工具是您使用CLI(命令行界面)而不是GUI(图形用户界面)运行的程序或工具。lsoflsof代表&...

幻隐说固态第一期:固态硬盘接口类别

前排声明所有信息来源于网络收集,如有错误请评论区指出更正。废话不多说,目前固态硬盘接口按速度由慢到快分有这几类:SATA、mSATA、SATAExpress、PCI-E、m.2、u.2。下面我们来...

新品轰炸 影驰SSD多款产品登Computex

分享泡泡网SSD固态硬盘频道6月6日台北电脑展作为全球第二、亚洲最大的3C/IT产业链专业展,吸引了众多IT厂商和全球各地媒体的热烈关注,全球存储新势力—影驰,也积极参与其中,为广大玩家朋友带来了...