如何在7分钟内快速完整地浏览Python3中的列表

img

  • 来源 | 愿码(ChainDesk.CN)内容编辑
  • 愿码Slogan | 连接每个程序员的故事
  • 网站 | http://chaindesk.cn
  • 愿码愿景 | 打造全学科IT系统免费课程,助力小白用户、初级工程师0成本免费系统学习、低成本进阶,帮助BAT一线资深工程师成长并利用自身优势创造睡后收入。
  • 官方公众号 | 愿码 | 愿码服务号 | 区块链部落
  • 免费加入愿码全思维工程师社群 | 任一公众号回复“愿码”两个字获取入群二维码

本文阅读时长:7min

Python列表与数组不同。在处理数组时,我们讨论了一组同类数据元素。对于python中的列表,情况并非如此。Python List可以存储异构的元素集合。此功能将帮助开发人员和程序员以更灵活的方式处理列表。python中的List是最强大的内置数据结构之一。

python中的列表还可以存储整数,浮点值,字符串,布尔值和复杂值。

如何在python中创建一个List


我们可以用两种方式在python中创建一个list

  1. 通过声明一个带有空方括号的变量 i.e []

  2. 通过使用list()。

# Here first I'm creating a my todo list which is used to store my to-do activities.
myTODOList = []
# The above line will create a list object for me
# I'm creating a another list which will store my general information.
myGeneralInfo = list()
# The above line will also create a list object for me
# Getting the types of list objects
print(type(myTODOList))
print(type(myGeneralInfo))

输出

输出上面几行代码

您可以使用最常用的方法创建新的列表对象。现在我们将继续讨论如何在列表中添加新元素以及更多内容。

如何将数据添加到列表?


首先,我想介绍一下Mutability的概念。可变性意味着改变其行为的能力。Python列表本质上是可变的。我们可以在列表中添加或删除元素。与其他内置数据结构相比,这是吸引程序员使用列表的最大优势之一。

我们可以通过两种方式向列表添加元素:

  1. 通过使用append()

  2. 通过使用insert()

通过使用append()

借助append方法,我们可以一次添加一个元素。此方法将帮助我们仅在列表的末尾添加元素。

append函数的语法是 -

listName.append(项目/元件)

# Adding Elements to the lists
myTODOList.append('Wake up Early Morning')
myTODOList.append('Go to Gym')
myTODOList.append('Play Some Games')
myTODOList.append('Get ready to go the college')
myTODOList.append('Go to library')
# Printing the entire list elements
print(myTODOList)

输出

输出上面的代码行

通过使用insert()

此插入方法用于在给定列表中的指定位置添加元素。

insert函数的语法是 -

listName.insert(position,item / element)

Insert()使用两个参数 - position和list item。该位置是元素需要保留在列表中的位置。这些位置通常称为索引。通常,python中的列表索引从0开始。(即第一个元素索引为0,第二个元素为1,第三个元素索引为2,依此类推)。由此,我们可以得出结论:

n个元素的列表最多具有n-1的索引号,即具有5个元素的列表将具有最大索引值4。

# Adding Elements to our list with the help of insert()
myGeneralInfo.insert(0, 'Paid the Library Fee')
myGeneralInfo.insert(1, 12000)
myGeneralInfo.insert(2, True)
myGeneralInfo.insert(3, 14+12j)
myGeneralInfo.insert(4, 3.141521)
# Printing the myGeneralInfo list information
print(myGeneralInfo)

输出

输出上面几行代码

如何访问列表元素


我们可以使用以下两种方式访问元素列表:

  1. 通过使用索引运算符。

  2. 通过使用切片运算符

通过使用索引运算符

我们可以在索引运算符的帮助下直接访问列表元素。

# Acessing the certain values from the list
print(myTODOList[1])
print(myTODOList[3])
print(myTODOList[4])

输出

上述计划的输出

通过使用切片运算符

切片运算符是有效访问列表元素的最常用运算符之一。slice运算符的语法是:

listName [start:stop:step]

start - 它表示切片必须开始的索引。默认值为0。

stop - 它表示切片必须结束的索引。默认值是列表的最大允许索引,即列表的长度。

step - 增加值。默认值为1。

# Getting the information using slice operator
print(myTODOList[0:3])  # we don't need to specify the step value.
print(myTODOList[2:4:1])
print(myTODOList[0:4:2])

输出

输出上面几行代码

Python列表是可迭代的对象。对于python中的任何可迭代对象,我们可以编写for循环来打印出所有数据。

# Iterating over the list
for item in myGeneralInfo:
      print(item)

输出上面的代码行

如何从列表中删除元素


我们可以通过以下两种方式删除列表元素:

  1. 通过使用remove()

  2. 通过使用pop()

通过使用remove()

remove()用于删除指定给它的元素。remove()的语法是:

listName.remove(项目/元件)

# Deleting the element from the list
myGeneralInfo.remove(12000)
myGeneralInfo.remove('Paid the Library Fee')
# printing the result after deleting the elements
print(myGeneralInfo)

删除列表元素后

通过使用pop()

它是一个迭代器方法,用于一次删除单个(或)多个元素。它从背面删除元素。pop()方法的语法是:

listName.pop()

# printing the list items before deleting
print('My TODO List Elements: ',myTODOList)
print('My General list Elements: ',myGeneralInfo)
# Deleting the list elements using pop()
myTODOList.pop()
myTODOList.pop()
# Deleting the list elements completely
for item in range(len(myGeneralInfo)):
       myGeneralInfo.pop()
# printing the results
print('My TODO List Elements: ',myTODOList)
print('My General list Elements: ',myGeneralInfo)

使用pop()删除列表元素的方式

在上面的程序中,我们在for循环中使用了len()。len()用于给出列表的长度,即列表中存在的元素的数量。

列表对象上的各种属性和函数


python dir()函数用于提供与之关联的内置属性和方法集。

# Printing all the attributes and functions on the list object
print(dir(myTODOList))

输出

列表对象上的各种属性和方法

各种列表方法及其用途:


1. append() - 它会在列表末尾添加一个元素。

2. clear() - 用于从列表中删除所有项目。

3. copy() - 用于返回列表的另一个副本。

4. count() - 用于返回作为参数传递的项数的计数。

5. extend() - 它将列表的所有元素添加到另一个列表中。

6. index() - 用于返回第一个匹配项的索引。

7. insert() - 用于在定义的索引处插入项目。

8. pop() - 用于删除和返回给定索引处的元素。

9. remove() - 用于从列表中删除项目。

10. reverse() - 用于反转列表中项目的顺序。

11. sort() - 用于按升序对列表中的项目进行排序。

何时使用列表数据结构?


如果要存储多个数据对象,则必须保留插入顺序。如果您还想存储重复值,那么此数据结构将更有助于执行此类操作。

猜你喜欢

转载自blog.csdn.net/weixin_43970764/article/details/89958450