1、list创建
new_list1 = ['TV','Car','Cloth','Food']
new_list2 = list(['TV','Car','Cloth','Food'])
print (new_list1)
print (new_list2)
运行结果:
['TV', 'Car', 'Cloth', 'Food']
['TV', 'Car', 'Cloth', 'Food']
2、list常用的方法:
1. append
new_list1 = ['TV','Car','Cloth','Food']
print (new_list1)
new_list1.append('Water') #追加'water'
print (new_list1)
运行结果:
['TV', 'Car', 'Cloth', 'Food']
['TV', 'Car', 'Cloth', 'Food', 'Water']
2.count
new_list1 = ['TV','Car','Cloth','Food','Food']
print (new_list1.count('Food')) #统计'food'在列表中的次数 => 2次
3.extend
new_list1 = ['TV','Car','Cloth','Food','Food']
new_list1.extend([11,22,33,'Car'])
print (new_list1)
运行结果:
['TV', 'Car', 'Cloth', 'Food', 'Food', 11, 22, 33, 'Car']
4.sort
new_list1 = ['TV','Car','Cloth','Food','Food']
print (new_list1)
new_list1.sort() #正向排序
print (new_list1)
new_list1.sort(reverse=True) #反向排序
print (new_list1)
运行结果:
['TV', 'Car', 'Cloth', 'Food', 'Food']
['Car', 'Cloth', 'Food', 'Food', 'TV'] #正向排序结果
['TV', 'Food', 'Food', 'Cloth', 'Car'] #反向排序结果
5. len
new_list1 = ['TV','Car','Food','Cloth','Food']
print (len(new_list1)) => 5 #打印元素的个数
6.remove
new_list1 = ['TV','Car','Food','Cloth','Food']
new_list1.remove('Food')
print (new_list1)
运行结果:
['TV', 'Car', 'Cloth', 'Food'] #remove会删除找到的第一个相同元素
7.pop
new_list1 = ['TV','Car','Food','Cloth','Food']
new_list1.pop() #随机删除一个元素
print (new_list1)
new_list1.pop(2) #指定删除某个元素
print (new_list1)
运行结果为:
['TV', 'Car', 'Food', 'Cloth']
['TV', 'Car', 'Cloth'] #,此处删除的是第3个元素'Food'
8.index
new_list1 = ['TV','Car','Food','Cloth','Food']
print (new_list1.index('Food')) #返回指定元素的index值,相同元素返回查找到的第一个
运行结果为:2
9.insert
new_list1 = ['TV','Car','Food','Cloth','Food']
new_list1.insert(2,'HAHAHA') #向指定的位置插入元素
print (new_list1)
运行结果为:
['TV', 'Car', 'HAHAHA', 'Food', 'Cloth', 'Food']

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

本文解释了如何使用美丽的汤库来解析html。 它详细介绍了常见方法,例如find(),find_all(),select()和get_text(),以用于数据提取,处理不同的HTML结构和错误以及替代方案(SEL)

本文比较了Tensorflow和Pytorch的深度学习。 它详细介绍了所涉及的步骤:数据准备,模型构建,培训,评估和部署。 框架之间的关键差异,特别是关于计算刻度的

Python的statistics模块提供强大的数据统计分析功能,帮助我们快速理解数据整体特征,例如生物统计学和商业分析等领域。无需逐个查看数据点,只需查看均值或方差等统计量,即可发现原始数据中可能被忽略的趋势和特征,并更轻松、有效地比较大型数据集。 本教程将介绍如何计算平均值和衡量数据集的离散程度。除非另有说明,本模块中的所有函数都支持使用mean()函数计算平均值,而非简单的求和平均。 也可使用浮点数。 import random import statistics from fracti

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

本文指导Python开发人员构建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等库详细介绍,强调输入/输出处理,并促进用户友好的设计模式,以提高CLI可用性。

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

文章讨论了虚拟环境在Python中的作用,重点是管理项目依赖性并避免冲突。它详细介绍了他们在改善项目管理和减少依赖问题方面的创建,激活和利益。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用