前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >7分钟内快速完整地浏览Python3中的列表

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

作者头像
一墨编程学习
发布2019-05-19 15:04:43
1.7K0
发布2019-05-19 15:04:43
举报
文章被收录于专栏:程序员的知识天地

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

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

如何在python中创建一个List

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

  1. 通过声明一个带有空方括号的变量 i.e []
  2. 通过使用list()。

代码语言:javascript
复制
# 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(项目/元件)

代码语言:javascript
复制
# 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。

代码语言:javascript
复制
# 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. 通过使用切片运算符

通过使用索引运算符

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

代码语言:javascript
复制
# 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。

代码语言:javascript
复制
# 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循环来打印出所有数据。

代码语言:javascript
复制
# Iterating over the list
for item in myGeneralInfo:
      print(item)
如何从列表中删除元素

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

  1. 通过使用remove()
  2. 通过使用pop()

通过使用remove()

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

listName.remove(项目/元件)

代码语言:javascript
复制
# 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()

代码语言:javascript
复制
# 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)

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

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

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

代码语言:javascript
复制
# 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() - 用于按升序对列表中的项目进行排序。

何时使用列表数据结构?

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.05.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 如何在python中创建一个List
  • 如何将数据添加到列表?
  • 如何访问列表元素
  • 如何从列表中删除元素
  • 列表对象上的各种属性和函数
  • 各种列表方法及其用途:
  • 何时使用列表数据结构?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档