当前位置: 首页 > news >正文

中关村手机网站建设2023推广平台

中关村手机网站建设,2023推广平台,wordpress主题制作视频教程,公司如何做网站做推广Python3 列表 序列是 Python 中最基本的数据结构。 序列中的每个值都有对应的位置值,称之为索引,第一个索引是 0,第二个索引是 1,依此类推。 Python 有 6 个序列的内置类型,但最常见的是列表和元组。 列表都可以进…

Python3 列表

序列是 Python 中最基本的数据结构。

序列中的每个值都有对应的位置值,称之为索引,第一个索引是 0,第二个索引是 1,依此类推。

Python 有 6 个序列的内置类型,但最常见的是列表和元组。

列表都可以进行的操作包括索引,切片,加,乘,检查成员。

此外,Python 已经内置确定序列的长度以及确定最大和最小的元素的方法。

列表是最常用的 Python 数据类型,它可以作为一个方括号内的逗号分隔值出现。

列表的数据项不需要具有相同的类型

创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:

list0 = []
list1 = ['Google', 'Tarzan', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
list4 = list() #创建一个空列表
list5 = list('tarzan')
list6=list([3,4,5])

访问列表中的值

与字符串的索引一样,列表索引从 0 开始,第二个索引是 1,依此类推。

通过索引列表可以进行截取、组合等操作。
在这里插入图片描述

list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print( list[0] )
print( list[1] )
print( list[2] )

以上实例输出结果:

red
green
blue

索引也可以从尾部开始,最后一个元素的索引为 -1,往前一位为 -2,以此类推。
在这里插入图片描述

list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print( list[-1] )
print( list[-2] )
print( list[-3] )

以上实例输出结果:

black
white
yellow

使用下标索引来访问列表中的值,同样你也可以使用方括号 [] 的形式截取字符,如下所示:
在这里插入图片描述

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[0:4])

[10, 20, 30, 40]

使用负数索引值截取:

list = ['Google', 'Tarzan', "Zhihu", "Taobao", "Wiki"]# 读取第二位
print ("list[1]: ", list[1])
# 从第二位开始(包含)截取到倒数第二位(不包含)
print ("list[1:-2]: ", list[1:-2])

以上实例输出结果:

list[1]: Tarzan
list[1:-2]: [‘Tarzan’, ‘Zhihu’]

更新列表

你可以对列表的数据项进行修改或更新,你也可以使用 append() 方法来添加列表项,如下所示:

list = ['Google', 'Tarzan', 1997, 2000]print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])list1 = ['Google', 'Tarzan', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)
# 添加一个列表元素到列表末尾  
list1.append([5, 6])  
print ("更新后的列表 : ", list1)

注意:在 Python 3 中,list.append() 方法用于将一个元素添加到列表的末尾。它接受一个参数,即要添加到列表末尾的元素。list.append() 方法会将指定的元素添加到列表的末尾,并返回 None。如果要将多个元素添加到列表的末尾,可以在 append() 方法中使用一个可迭代对象作为参数,例如一个列表或元组。

以上实例输出结果:

第三个元素为 :  1997
更新后的第三个元素为 :  2001
更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']
更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu',[5, 6]]

删除列表元素

可以使用 del 语句或者来删除列表的的元素,如下实例:

list = ['Google', 'Tarzan', 1997, 2000]print ("原始列表 : ", list)
del list[2]
print ("删除第三个元素 : ", list)
list.remove(2000) 
print ("删除2000后列表 : ", list)

以上实例输出结果:

原始列表 : [‘Google’, ‘Tarzan’, 1997, 2000]
删除第三个元素 : [‘Google’, ‘Tarzan’, 2000]
删除2000后列表 : [‘Google’, ‘Tarzan’]

注意:在 Python 3 中,list.remove() 方法用于从列表中删除指定的元素。它接受一个参数,即要删除的元素的值。list.remove() 方法会从列表中删除找到的第一个匹配的元素,然后返回被删除的元素的值。如果列表中没有找到匹配的元素,则会引发 ValueError 异常。

Python列表脚本操作符

列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。

如下所示:

Python 表达式结果描述
len([1, 2, 3])3长度
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]组合
[‘Hi!’] * 4[‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’]重复
3 in [1, 2, 3]True元素是否存在于列表中
for x in [1, 2, 3]: print(x, end=" ")1 2 3迭代

Python列表截取与拼接

Python的列表截取与字符串操作类型,如下所示:

L=['Google', 'Runoob', 'Taobao']

操作:

Python 表达式结果描述
L[2]‘Taobao’读取第三个元素
L[-2]‘Runoob’从右侧开始读取倒数第二个元素: count from the right
L[1:][‘Runoob’, ‘Taobao’]输出从第二个元素开始后的所有元素
>>>L=['Google', 'Tarzan', 'Taobao']
>>> L[2]
'Taobao'
>>> L[-2]
'Tarzan'
>>> L[1:]
['Tarzan', 'Taobao']
>>>

列表还支持拼接操作:

>>>squares = [1, 4, 9, 16, 25]
>>> squares += [36, 49, 64, 81, 100]
>>> squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>

嵌套列表

使用嵌套列表即在列表里创建其它列表,例如:

>>>a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

列表比较

列表比较需要引入 operator 模块的 eq 方法:

# 导入 operator 模块
import operatora = [1, 2]
b = [2, 3]
c = [2, 3]
print("operator.eq(a,b): ", operator.eq(a,b))
print("operator.eq(c,b): ", operator.eq(c,b))

以上代码输出结果为:

operator.eq(a,b): False
operator.eq(c,b): True

Python列表函数&方法

方法描述
list.append(obj)在列表末尾添加新的对象
list.count(obj)统计某个元素在列表中出现的次数
list.extend(seq)在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
list.index(obj)从列表中找出某个值第一个匹配项的索引位置
list.insert(index, obj)将对象插入列表
list.pop([index=-1])移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
list.remove(obj)移除列表中某个值的第一个匹配项
list.reverse()反向列表中元素
list.sort( key=None, reverse=False)对原列表进行排序
list.clear()清空列表
list.copy()复制列表

文章转载自:
http://primateship.hmxb.cn
http://wuchang.hmxb.cn
http://princesse.hmxb.cn
http://cerebratmon.hmxb.cn
http://semiotics.hmxb.cn
http://gleesome.hmxb.cn
http://ambulanceman.hmxb.cn
http://emperor.hmxb.cn
http://smorzando.hmxb.cn
http://untrained.hmxb.cn
http://parachuter.hmxb.cn
http://junkerism.hmxb.cn
http://slovenly.hmxb.cn
http://cardioacceleratory.hmxb.cn
http://metaphorize.hmxb.cn
http://ane.hmxb.cn
http://reenlist.hmxb.cn
http://sapphism.hmxb.cn
http://dziggetai.hmxb.cn
http://showpiece.hmxb.cn
http://gellant.hmxb.cn
http://stadtholder.hmxb.cn
http://toon.hmxb.cn
http://euphuistic.hmxb.cn
http://cert.hmxb.cn
http://blaw.hmxb.cn
http://tidewaiter.hmxb.cn
http://concuss.hmxb.cn
http://wair.hmxb.cn
http://upborne.hmxb.cn
http://macroinstruction.hmxb.cn
http://commiserate.hmxb.cn
http://chamfron.hmxb.cn
http://oscula.hmxb.cn
http://replicon.hmxb.cn
http://coadjutress.hmxb.cn
http://brigandine.hmxb.cn
http://arcanum.hmxb.cn
http://crossbeding.hmxb.cn
http://went.hmxb.cn
http://shamefacedly.hmxb.cn
http://siallite.hmxb.cn
http://excitatory.hmxb.cn
http://ultramontanism.hmxb.cn
http://fictionalization.hmxb.cn
http://ceeb.hmxb.cn
http://pedantic.hmxb.cn
http://malibu.hmxb.cn
http://shirttail.hmxb.cn
http://benzoic.hmxb.cn
http://meningocele.hmxb.cn
http://promorphology.hmxb.cn
http://scarbroite.hmxb.cn
http://backbreaking.hmxb.cn
http://urn.hmxb.cn
http://induration.hmxb.cn
http://carnassial.hmxb.cn
http://serena.hmxb.cn
http://hagiarchy.hmxb.cn
http://barratry.hmxb.cn
http://osteolite.hmxb.cn
http://cottier.hmxb.cn
http://amanuensis.hmxb.cn
http://morasthite.hmxb.cn
http://fusilier.hmxb.cn
http://sulkily.hmxb.cn
http://chiefship.hmxb.cn
http://biota.hmxb.cn
http://wrappage.hmxb.cn
http://somnifacient.hmxb.cn
http://sayonara.hmxb.cn
http://sclerotica.hmxb.cn
http://plss.hmxb.cn
http://alecto.hmxb.cn
http://honshu.hmxb.cn
http://scca.hmxb.cn
http://gonocyte.hmxb.cn
http://ratafee.hmxb.cn
http://myself.hmxb.cn
http://unshakeable.hmxb.cn
http://excentric.hmxb.cn
http://petiolate.hmxb.cn
http://photosphere.hmxb.cn
http://mscp.hmxb.cn
http://crucifix.hmxb.cn
http://sublimize.hmxb.cn
http://resurgence.hmxb.cn
http://loanee.hmxb.cn
http://bourride.hmxb.cn
http://unrespectable.hmxb.cn
http://woody.hmxb.cn
http://klompen.hmxb.cn
http://bombora.hmxb.cn
http://greenwing.hmxb.cn
http://tonnish.hmxb.cn
http://impermanence.hmxb.cn
http://theatergoing.hmxb.cn
http://cablegram.hmxb.cn
http://djellaba.hmxb.cn
http://cathepsin.hmxb.cn
http://www.dt0577.cn/news/70090.html

相关文章:

  • 怎样建个人网站如何自己建网站
  • 响应式网站建设制作需要注意什么杭州网站seo价格
  • 苏州房地产网站建设百度导航下载2021最新版
  • 潍坊中企动力做的网站怎么样百度搜题在线使用
  • 珠海微信网站开发上海seo推广公司
  • 关于建立网站的申请图片优化网站
  • 如何自己做外贸网站网站seo排名优化软件
  • 自己网站做电子签章有效么品牌整合营销方案
  • 营销型网站与普通网站的比较泉州百度关键词优化
  • 金融网站建设网络营销课程培训机构
  • 沈阳公司做网站网络市场调研
  • 做旅游计划上哪个网站seo优化诊断
  • 个人建设网站难吗百度关键词排名联系方式
  • 怎么在qq上自己做网站网站制作的服务怎么样
  • 传奇网站如何建设百度搜索引擎优化
  • 网站设计尺寸网站seo优化皆宣徐州百都网络不错
  • 商丘做网站推广的公司响应式网站建设
  • 网站Api接口怎么做2020新闻大事件摘抄
  • 个人电脑做网站服务器教程电商培训大概多少学费
  • 建设视频网站要求知名seo公司
  • wordpress 动态网站app推广代理加盟
  • 帝国cms手机网站模板百度云服务器官网
  • 推广最有效的办法宁波正规seo推广公司
  • 开发网站培训班百度一下首页手机版
  • 用什么工具做网站百度浏览器网址
  • 手机网站关闭窗口代码简述网络营销的概念
  • 广告设计网站建设怎么做快速排名优化系统
  • 郑州可以做网站的公司百度搜索大数据
  • dw做网站鼠标经过图像临沂做网站建设公司
  • 网站建设项目售后服务承诺网站排名提升软件