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

一文掌握Python中列表(python中列表常用方法)

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

创建列表

可以在Python中创建一个列表,方法是将一系列元素放在方括号( [] )中,用逗号分隔。

# Example of a list of numbers
numbers = [1, 2, 3, 4, 5]

# Example of a list of strings
fruits = ["apple", "banana", "cherry"]

# Example of a mixed-type list
mixed = [1, "hello", 3.14, True]

元素访问

可以使用列表中的元素的索引来访问它们。第一个元素的索引从0开始。

numbers = [1, 2, 3, 4, 5]

first_element = numbers[0]  # Output: 1
second_element = numbers[1]  # Output: 2

列表操作函数和方法

添加元素

可以使用各种方法向列表添加元素:

# Append: Adds an element to the end of the list
fruits = ["apple", "banana"]
fruits.append("cherry")  # fruits is now ["apple", "banana", "cherry"]

# Insert: Adds an element at a specific index
fruits.insert(1, "orange")  # fruits is now ["apple", "orange", "banana", "cherry"]

扩展列表

将元素从另一个列表移到当前列表的末尾。

fruits = ["apple", "banana"]
more_fruits = ["cherry", "orange"]
fruits.extend(more_fruits)  # fruits is now ["apple", "banana", "cherry", "orange"]

更改项目值

fruits = ["apple", "banana", "cherry", "banana"]
fruits[1]="melon"  # fruits is now ['apple', 'melon', 'cherry', 'banana']

fruits[1:3]=["pappaya","kiwi"] # fruits is now ['apple', 'pappaya', 'kiwi', 'banana']

移除元素

可以使用各种方法从列表中删除元素:

# Remove: Removes the first occurrence of a value
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")  # fruits is now ["apple", "cherry", "banana"]

# Pop: Removes and returns the element at a specific index
removed_element = fruits.pop(1)  # removed_element is "cherry", fruits is now ["apple", "banana"]

# clear : Removes all elements from the list, making it empty.
fruits = ["apple", "banana", "cherry"]
fruits.clear()  # fruits is now []

# del : deleting the elements or entire list
del mylist[0] # deleting the first elemet
del mylist # deleting the entire elemet

复制列表

创建列表的副本。

fruits = ["apple", "banana", "cherry"]
copy_fruits = fruits.copy()  # copy_fruits is a separate copy of fruits
# or
copy_fruits = fruits[:]  # Another way to create a copy of the list

逆转列表

反转列表中元素的顺序。

numbers = [1, 2, 3, 4, 5]
numbers.reverse()  # numbers is now [5, 4, 3, 2, 1]

排序列表

按升序对列表中的元素进行排序(默认情况下)。

numbers = [5, 1, 3, 2, 4]
numbers.sort()  # numbers is now [1, 2, 3, 4, 5]

# Sorting in descending order
numbers.sort(reverse = True)  # numbers is now [5, 4, 3, 2, 1]

计数

返回指定值在列表中出现的次数。

numbers = [1, 2, 3, 4, 2, 2, 5]
count = numbers.count(2)  # count is 3

嵌套列表

嵌套列表是包含其他列表作为其元素的列表。这允许创建列表的列表,提供了一种表示更复杂数据结构的方法。在Python中,你可以嵌套列表来创建多维数据结构。

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

在这个例子中, nested_list 包含三个内部列表,每个列表代表一行数据:

[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]]

以下是如何在Python中使用嵌套列表:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Accessing elements
element = nested_list[1][1]  # element is 5

# Modifying elements
nested_list[0][2] = 10  # nested_list is now [[1, 2, 10], [4, 5, 6], [7, 8, 9]]

# Adding a new inner list
new_inner_list = [11, 12, 13]
nested_list.append(new_inner_list)

# Removing an inner list
nested_list.remove([4, 5, 6])

# Displaying the nested list
for inner_list in nested_list:
    print(inner_list)

输出量:

[1, 2, 10]
[7, 8, 9]
[11, 12, 13]

相关推荐

面试官:来,讲一下枚举类型在开发时中实际应用场景!

一.基本介绍枚举是JDK1.5新增的数据类型,使用枚举我们可以很好的描述一些特定的业务场景,比如一年中的春、夏、秋、冬,还有每周的周一到周天,还有各种颜色,以及可以用它来描述一些状态信息,比如错...

一日一技:11个基本Python技巧和窍门

1.两个数字的交换.x,y=10,20print(x,y)x,y=y,xprint(x,y)输出:102020102.Python字符串取反a="Ge...

Python Enum 技巧,让代码更简洁、更安全、更易维护

如果你是一名Python开发人员,你很可能使用过enum.Enum来创建可读性和可维护性代码。今天发现一个强大的技巧,可以让Enum的境界更进一层,这个技巧不仅能提高可读性,还能以最小的代价增...

Python元组编程指导教程(python元组的概念)

1.元组基础概念1.1什么是元组元组(Tuple)是Python中一种不可变的序列类型,用于存储多个有序的元素。元组与列表(list)类似,但元组一旦创建就不能修改(不可变),这使得元组在某些场景...

你可能不知道的实用 Python 功能(python有哪些用)

1.超越文件处理的内容管理器大多数开发人员都熟悉使用with语句进行文件操作:withopen('file.txt','r')asfile:co...

Python 2至3.13新特性总结(python 3.10新特性)

以下是Python2到Python3.13的主要新特性总结,按版本分类整理:Python2到Python3的重大变化Python3是一个不向后兼容的版本,主要改进包括:pri...

Python中for循环访问索引值的方法

技术背景在Python编程中,我们经常需要在循环中访问元素的索引值。例如,在处理列表、元组等可迭代对象时,除了要获取元素本身,还需要知道元素的位置。Python提供了多种方式来实现这一需求,下面将详细...

Python enumerate核心应用解析:索引遍历的高效实践方案

喜欢的条友记得关注、点赞、转发、收藏,你们的支持就是我最大的动力源泉。根据GitHub代码分析统计,使用enumerate替代range(len())写法可减少38%的索引错误概率。本文通过12个生产...

Python入门到脱坑经典案例—列表去重

列表去重是Python编程中常见的操作,下面我将介绍多种实现列表去重的方法,从基础到进阶,帮助初学者全面掌握这一技能。方法一:使用集合(set)去重(最简单)pythondefremove_dupl...

Python枚举类工程实践:常量管理的标准化解决方案

本文通过7个生产案例,系统解析枚举类在工程实践中的应用,覆盖状态管理、配置选项、错误代码等场景,适用于Web服务开发、自动化测试及系统集成领域。一、基础概念与语法演进1.1传统常量与枚举类对比#传...

让Python枚举更强大!教你玩转Enum扩展

为什么你需要关注Enum?在日常开发中,你是否经常遇到这样的代码?ifstatus==1:print("开始处理")elifstatus==2:pri...

Python枚举(Enum)技巧,你值得了解

枚举(Enum)提供了更清晰、结构化的方式来定义常量。通过为枚举添加行为、自动分配值和存储额外数据,可以提升代码的可读性、可维护性,并与数据库结合使用时,使用字符串代替数字能简化调试和查询。Pytho...

78行Python代码帮你复现微信撤回消息!

来源:悟空智能科技本文约700字,建议阅读5分钟。本文基于python的微信开源库itchat,教你如何收集私聊撤回的信息。[导读]Python曾经对我说:"时日不多,赶紧用Python"。于是看...

登录人人都是产品经理即可获得以下权益

文章介绍如何利用Cursor自动开发Playwright网页自动化脚本,实现从选题、写文、生图的全流程自动化,并将其打包成API供工作流调用,提高工作效率。虽然我前面文章介绍了很多AI工作流,但它们...

Python常用小知识-第二弹(python常用方法总结)

一、Python中使用JsonPath提取字典中的值JsonPath是解析Json字符串用的,如果有一个多层嵌套的复杂字典,想要根据key和下标来批量提取value,这是比较困难的,使用jsonpat...

取消回复欢迎 发表评论: