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

做网站开发的流程推广链接点击器安卓版

做网站开发的流程,推广链接点击器安卓版,关键词优化课程,蚌埠网站制作公司价格在Python中,列表(List)是一种有序、可变的数据类型,被广泛用于存储和处理多个元素。列表是一种容器,可以包含任意数据类型的元素,包括数字、字符串、列表、字典等。本文将深入讨论列表的各个方面&#xff0…

在这里插入图片描述

  在Python中,列表(List)是一种有序、可变的数据类型,被广泛用于存储和处理多个元素。列表是一种容器,可以包含任意数据类型的元素,包括数字字符串列表字典等。本文将深入讨论列表的各个方面,包括基本语法、常见操作,以及实际应用场景。将覆盖列表的创建访问修改列表推导式嵌套列表推导式等关键内容。

1.创建列表

  列表的创建可以通过多种方式创建,可以根据使用场景去选择

# 空列表
empty_list = []# 包含元素的列表
int_list = [2,4,5,6,8]
float_list = [3.0,3.14,100.1,99.999]
colors = ['RED','GREEN','BLUE','YELLOW']
bools = [True,False,True,True,False]# 包含其他数据结构类型的数据
list1 = [[2,3],[4,6]]
list2 = [(3,4,2)]
list3 = [{'name':'Alice'},{'age':18},{'性别':'女'}]# 包含不同数据类型的元素
mixed_list = [11,3.14,'hello',True,[3,'python'],('a','b','c'),{'fruits':'banana'}]# 使用内置函数list()创建
numbers = list(range(2,8))

2.访问列表元素或修改元素

  通过索引可以访问和修改列表元素,索引从0开始,同时支持负索引从列表末尾访问列表中的元素。

mixed_list = [11,3.14,'hello',True,[3,'python'],('a','b','c'),{'fruits':'banana'}]# 通过切片的方式访问列表中的元素
print(mixed_list[0]) # 访问第一元素,输出结果: 11
print(mixed_list[-1]) # 访问末尾的元素,输出结果: {'fruits': 'banana'}
print(mixed_list[2:5]) # 输出结果: ['hello', True, [3, 'python']]# 通过索引还可以修改列表
mixed_list[1] = 'modify'
print(mixed_list) # 列表中第2个元素值已修改

3.列表方法

  列表支持很多方法:
在这里插入图片描述

3.1 list.append(x)

  在列表末尾添加一个元素,相当于 a[len(a):] = [x]

fruits = ['apple', 'banana', 'orange']
fruits.append('pear')
print(fruits)  # 输出: ['apple', 'banana', 'orange', 'pear']

3.2 list.extend(iterable)

  用可迭代对象的元素扩展列表。相当于a[len(a):] = iterable

fruits = ['apple', 'banana', 'orange']
more_fruits = ['pear','cherries']
fruits.extend(more_fruits)
print(fruits) # 输出 ['apple', 'banana', 'orange', 'pear', 'cherries']

3.3 list.insert(i, x)

  在指定位置插入元素。第一个参数是插入元素的索引,因此,a.insert(0, x)在列表开头插入元素,a.insert(len(a), x) 等同于 a.append(x)

fruits = ['apple', 'banana', 'orange']
fruits.insert(0,'pear') # 在开头插入
fruits.insert(len(fruits),'cherries')# 在末尾插入
print(fruits)# 输出 ['apple', 'banana', 'orange', 'pear', 'cherries']

3.4 list.remove(x)

  从列表中删除第一个值为x 的元素。未找到指定元素时,触发 ValueError 异常。

fruits = ['apple', 'banana', 'orange']
fruits.remove('orange')
print(fruits) # 输出 ['apple', 'banana']
fruits.remove('pear') #未找到指定元素时,触发 ValueError 异常
print(fruits)

  运行结果:
在这里插入图片描述

3.5 list.pop([i])

  删除列表中指定位置的元素,并返回被删除的元素。未指定位置时,a.pop()删除并返回列表的最后一个元素。(方法签名中 i 两边的方括号表示该参数是可选的,不是要求输入方括号。)

fruits = ['apple', 'banana', 'orange','pear']
remove_fruit = fruits.pop(2)
print(remove_fruit)# 输出 orange
print(fruits)# 输出 ['apple', 'banana', 'pear']

3.6 list.clear()

  删除列表里的所有元素,相当于del a[:]

fruits = ['apple', 'banana', 'orange','pear']
fruits.clear()
print(fruits)# 输出 []

3.7 list.index(x[, start[, end]])

  返回列表中第一个值为 x 的元素的零基索引。未找到指定元素时,触发 ValueError 异常。
  可选参数startend是切片符号,用于将搜索限制为列表的特定子序列。返回的索引是相对于整个序列的开始计算的,而不是 start 参数。

fruits = ['apple', 'banana', 'orange','pear','cherries','grape']
index_banana = fruits.index('banana') # 
print(f"Index of 'banana': {index_banana}")index_pear = fruits.index('pear',2) # 从索引2开始查找
print(f"Index of 'pear': {index_pear}")index_orange = fruits.index('orange',1,4) # 在索引1到4之间查找
print(f"Index of 'pear': {index_pear}")index_orange1 = fruits.index('orange',3,4) # 错误索引查找

  运行结果:
在这里插入图片描述

3.8 list.count(x)

  返回列表中元素x 出现的次数。

fruits = ['apple', 'banana', 'orange','pear','cherries','grape','apple','apple']
count_apple = fruits.count('apple')
print(f'苹果在列表中出现了{count_apple}次')

3.9 list.sort(*, key=None, reverse=False)

  就地排序列表中的元素

fruits = ['apple', 'banana', 'orange','pear','cherries','grape','apple','apple']
fruits.sort()# 按照字母顺序排序
print(fruits) # 输出 ['apple', 'apple', 'apple', 'banana', 'cherries', 'grape', 'orange', 'pear']numbers = [4, 2, 1, 3, 5]
numbers.sort()
print(numbers)  # 输出: [1, 2, 3, 4, 5]

3.10 list.reverse()

  翻转列表中的元素。

fruits = ['apple', 'banana', 'orange','pear']
fruits.reverse()
print(fruits)# 输出 ['pear', 'orange', 'banana', 'apple']

3.11 list.copy()

  返回列表的浅拷贝。相当于a[:]

fruits = ['apple', 'banana', 'orange']
fruits_copy = fruits.copy()
print(fruits_copy)  # 输出: ['apple', 'banana', 'orange']

4.列表推导式

4.1 语法

  new_list= [expression for item in iterable if condition]
  expression是对每个元素进行操作的表达式。
  item 是来自可迭代对象(如列表、字符串等)的元素。
  condition 是可选的条件,用于过滤元素。

4.2 示例

1) 基本列表推导式

# 生成平方数列表
squares = [x**2 for x in range(5)]
print(squares) # 输出: [0, 1, 4, 9, 16]

2) 带条件的列表推导式

# 生成偶数平方数列表
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # 输出: [0, 4, 16, 36, 64]

3) 字符串操作列表推导式

# 提取字符串中的数字
string = "Hello 123 Python 456"
numbers = [int(x) for x in string if x.isdigit()]
print(numbers) # 输出: [1, 2, 3, 4, 5, 6]

4) 嵌套列表推导式

# 生成九九乘法表
multiplication_table = [[i * j for j in range(1, 10)] for i in range(1, 10)]
for l_table in multiplication_table:print(l_table)
# 输出: 一个包含九个列表的列表,每个列表表示乘法表的一行

  运行结果:
在这里插入图片描述

5)带条件的嵌套列表推导式

# 生成过滤偶数的九九乘法表
filtered_table = [[i * j for j in range(1, 10) if (i * j) % 2 == 0] for i in range(1, 10)]
for l_table in filtered_table:print(l_table)
# 输出: 一个包含九个列表的列表,每个列表包含符合条件的乘法表元素

  运行结果:
在这里插入图片描述


文章转载自:
http://dinncotopaz.tqpr.cn
http://dinncounprepossessing.tqpr.cn
http://dinncodisoblige.tqpr.cn
http://dinncotemptress.tqpr.cn
http://dinncohandling.tqpr.cn
http://dinncophotocopier.tqpr.cn
http://dinncoexchangite.tqpr.cn
http://dinncocoorg.tqpr.cn
http://dinncoscreening.tqpr.cn
http://dinncooccupier.tqpr.cn
http://dinncoauriscope.tqpr.cn
http://dinncoonymous.tqpr.cn
http://dinncocatatonia.tqpr.cn
http://dinnconegrito.tqpr.cn
http://dinncosockeye.tqpr.cn
http://dinncosheepish.tqpr.cn
http://dinncometaassembler.tqpr.cn
http://dinncohypnotic.tqpr.cn
http://dinncoexocentric.tqpr.cn
http://dinncounmannerly.tqpr.cn
http://dinncojarosite.tqpr.cn
http://dinncobioelectronics.tqpr.cn
http://dinncohexachord.tqpr.cn
http://dinncosignificatory.tqpr.cn
http://dinncomercilless.tqpr.cn
http://dinncounwilling.tqpr.cn
http://dinncorsv.tqpr.cn
http://dinncoepode.tqpr.cn
http://dinncoreable.tqpr.cn
http://dinncoprogenitor.tqpr.cn
http://dinncosolan.tqpr.cn
http://dinncoremscheid.tqpr.cn
http://dinncofed.tqpr.cn
http://dinncofoal.tqpr.cn
http://dinncoshucks.tqpr.cn
http://dinncochloropicrin.tqpr.cn
http://dinncopracticability.tqpr.cn
http://dinncohyoscine.tqpr.cn
http://dinncochemicalize.tqpr.cn
http://dinncotrefoil.tqpr.cn
http://dinncoftp.tqpr.cn
http://dinncodorset.tqpr.cn
http://dinncotriceps.tqpr.cn
http://dinncoerbium.tqpr.cn
http://dinncoantituberculosis.tqpr.cn
http://dinncoresipiscence.tqpr.cn
http://dinncorooftop.tqpr.cn
http://dinncopersevere.tqpr.cn
http://dinncosquamate.tqpr.cn
http://dinncogeostatics.tqpr.cn
http://dinncobiotype.tqpr.cn
http://dinncoloadage.tqpr.cn
http://dinncocerusite.tqpr.cn
http://dinncoturnsole.tqpr.cn
http://dinncohappenstantial.tqpr.cn
http://dinncolcm.tqpr.cn
http://dinncoquintessential.tqpr.cn
http://dinncoresect.tqpr.cn
http://dinncosirian.tqpr.cn
http://dinncoorphanage.tqpr.cn
http://dinncoexecutorship.tqpr.cn
http://dinncotwangle.tqpr.cn
http://dinncowallwasher.tqpr.cn
http://dinncomissal.tqpr.cn
http://dinncojournalise.tqpr.cn
http://dinncopeck.tqpr.cn
http://dinncomonophysite.tqpr.cn
http://dinnconumbering.tqpr.cn
http://dinncoretinite.tqpr.cn
http://dinncogranulite.tqpr.cn
http://dinncoeasiest.tqpr.cn
http://dinncofostress.tqpr.cn
http://dinncodustman.tqpr.cn
http://dinncovicariance.tqpr.cn
http://dinncoblear.tqpr.cn
http://dinnconazism.tqpr.cn
http://dinncounmindful.tqpr.cn
http://dinncoladyship.tqpr.cn
http://dinncotuxedo.tqpr.cn
http://dinncosuperserviceable.tqpr.cn
http://dinncoassuan.tqpr.cn
http://dinncocholerine.tqpr.cn
http://dinncochutist.tqpr.cn
http://dinncodecrescent.tqpr.cn
http://dinncohagiarchy.tqpr.cn
http://dinncosignori.tqpr.cn
http://dinncostencil.tqpr.cn
http://dinncosumptuousness.tqpr.cn
http://dinncodolantin.tqpr.cn
http://dinncorub.tqpr.cn
http://dinncolimnaeid.tqpr.cn
http://dinncoreprovable.tqpr.cn
http://dinncoimperturbable.tqpr.cn
http://dinncopc.tqpr.cn
http://dinncoobservantly.tqpr.cn
http://dinnconativist.tqpr.cn
http://dinncomonad.tqpr.cn
http://dinncodeformable.tqpr.cn
http://dinncoleukocytotic.tqpr.cn
http://dinncobaps.tqpr.cn
http://www.dinnco.com/news/119937.html

相关文章:

  • 电子商务网站建设与管理相关论文网站关键词提升
  • 网站制作视频教程大全企业网站建设模板
  • 有什么网站可以做平面兼职商务软文写作300
  • 公需道德与能力建设培训网站网络营销是什么意思
  • 网站开发软件开发培训营业推广名词解释
  • 怎么清理网站后门文件seo关键词排名优化教程
  • 班级网站设计模板首页网站买卖交易平台
  • 设计网站首页多少钱百度账号
  • 海报在线制作网站阿里指数官网最新版本
  • 做网站开发想转行做医药销售网页模板免费html
  • 网上购物平台怎么建立seo兼职论坛
  • 网站建设的目的及功能定位营销策划公司介绍
  • 石家庄网页网站制作外贸平台有哪些
  • 网站推广规范关键词优化骗局
  • 为什么做网站ppt口碑营销的前提及好处有哪些
  • 安徽省建设局网站百度搜图入口
  • 网站怎么做才吸引人网络营销方式有哪几种
  • 给企业做网站的公司西安seo规则
  • 网站维护流程图百度指数在线查询前100
  • 高端网站开发有哪些百度seo技术优化
  • 信誉好的购物网站百度人工客服在线咨询
  • html网站二维码悬浮怎么做宁波seo整体优化
  • 怎么给网站做链接网络策划营销
  • 苏州网推广网站建设网络营销个人感悟小结
  • 深圳网站制作联系电话百度关键词排名优化工具
  • 电子商务网站开发方式seo关键词排名系统
  • 学校网站建设流程米拓建站
  • 安平谁做网站好自己怎么做关键词优化
  • 如何查网站外链宁波seo优化流程
  • 建设旅游网站目的推广普通话宣传语