Python入门(三)
来源:互联网 发布:淘宝内衣模特偷拍网盘 编辑:程序博客网 时间:2024/06/04 18:59
LIST
列表是一个数据的集合,集合内可以放任何数据类型,可对集合进行方便的增删改查操作。
list类型的声明
months=[]
print(type(months))
names=[‘shanshan’,’longting’]append函数,list中添加元素
months.append(1)
months.append(“January”)
months.append(2)
months.append(“February”)在list中找一个值,创建list的时候就产生了对应的索引,从0开始
countries=[]
temperatures=[]
countries.append(“china”)
countries.append(“india”) countries.append(“united states”)
temperatures.append(30.5)
temperatures.append(25.0)
temperatures.append(15.1)
china=countries[0]
china_temperature=temperature[0]len函数,表示list中有多少元素
int_months=[1,2,3,4,5,6,7,8,9,10,11,12]
length=len(int_months)
index=len(int_months)-1
last_value=int_months[index]
print(last_value) #12
print(int_months[-1]) #12-1代表倒数第一个元素,-2代表倒数第二个元素……
切片,取list中某个片段
months=[“jan”,”feb”,”mar”,”apr”,”may”,”jun”,”jul”]
two_four=months[2:4] #values at index 2,3,but not 4,取头不取尾
print(two_four) #[“mar”,”apr”]
three_six=months[3:] #一直取到尾
print(three_six) #[“apr”,”may”,”jun”,”jul”]
two_four=months[2:4:2] #第二个2代表隔两个取值,步长
- index函数,找索引值
countries=[]countries.append("china")countries.append("india") countries.append("united states")a=countries.index(India)country=countries[a]
7.count函数,统计个数
n2=['shanshan','longting','alex',1,3,4,4,5,6,7,7,8,8,9,9,0,0,2,3,4,4]num=n2.count(4) #统计4有几个
8.插入,insert函数,给索引i处插入值
n2=['shanshan','longting','alex',1,3,4,4,5,6,7,7,8,8,9,]n2.insert(0,'abc')#['abc','shanshan','longting','alex',1,3,4,4,5,6,7,7,8,8,9,4]n2[2]='peiqi' #修改n2[2:4]='peiqi' #批量修改
9.pop函数,删除最后一个,remove函数删除,del函数批量删除
n2=['shanshan','longting','alex',1,3,4,4,5,6,7,7,8,8,9,]n2.pop()n2.remove('shanshan')del n2[2:4]
10.循环
n2=['shanshan','longting','alex',1,3,4,4,5,6,7,7,8,8,9,]for i in n2: print(i)
11.排序,sort函数,按ascii码排序;reverse倒序排序;
n2.sort()
12.扩展,+或者extend函数
n1=[1,3,4]n2=['shanshan','longting','alex',1,3,4,4,5,6,]n=n2+n1n2.extend(n1)#['shanshan','longting','alex',1,3,4,4,5,6,1,3,4]
13.clear函数,copy函数
n2.clear()n3=n2.copy()
14.枚举enumerate函数
names=['alex','shanshan']for index,i in enumerrate(names): print(index,i)
#0 alex
#1 shanshan
- python入门(三)
- python入门(三)
- python入门(三)
- Python入门(三)
- PYTHON入门(三)
- Python入门(三,初级)
- python 入门笔记(三)
- Python入门基础(三)
- Python入门基础(三)
- Python学习(三)入门技巧
- Python爬虫入门(三)BeautifulSoup库
- python初学者快速入门(三)字符串
- 代码Python入门(三、运算符)
- python入门三(多线程,多进程)
- python入门(三)条件语句语法
- Python入门基础之函数(三)
- Python 从入门到放弃(三)
- 【python入门笔记】(三)基本数据结构
- 运算符的运用
- [BZOJ] 1303
- FLASK ON IIS
- strcat()函数原代码解析
- Nginx源码剖析之内存池、内存管理
- Python入门(三)
- NOIP2017滚粗记
- Windows7系统禁用驱动程序签名强制的方法
- The system is running in low-graphics mode
- golang+walk根据数据库表生成java,proto和模板
- 爬取Ajax界面
- 部署Nginx反向代理三个web服务并调度算法使用加权轮询
- Spring事务的传播行为
- JAVA获取OppenId