Python学习笔记_Python基础二
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习笔记_Python基础二相关的知识,希望对你有一定的参考价值。
列表、元祖:
创建列表
names = ["Eric","Tony","Jack"]
通过列表的下标访问列表中的元素,下标从0开始
>>> names[0] ‘Eric‘ >>> names[1] ‘Tony‘ >>> names[2] ‘Jack‘ >>> names[-1]#还可以倒着取 ‘Jack‘ >>> names[-2] ‘Tony‘
列表切片取多个元素
>>> names = ["Eric","Tony","Jack","Rain","Tom"] >>> names[1:4] #取下标1至下标4之间的元素,包括1,但不包括4 ["Tony","Jack","Rain","Tom"] >>> names[1:-1] #取下标1至-1的元素,不包括-1 ["Tony","Jack","Rain","Tom"] >>> names[0:3] ["Eric","Tony","Jack"] >>> names[:3] #如果是从头开始取,0可以忽略,跟上句效果一样 ["Eric","Tony","Jack"] >>> names[3:] #如果想取最后一个,必须不能写-1,只能这么写 ["Rain","Tom"] >>> names[3:-1] #这样-1就不会被包含了 [‘Rain‘] >>> names[0::2] #后面的2是代表,每隔一个元素,就取一个 ["Eric","Jack","Tom"] >>> names[::2] #和上句效果一样 ["Eric","Jack","Tom"]
列表追加 names.append("xxxx")
>>> names = ["Eric","Tony","Jack","Rain","Tom"] ["Eric","Tony","Jack","Rain","Tom"] >>> names .append("大魔王") >>> names >>> names = ["Eric","Tony","Jack","Rain","Tom","大魔王"]
列表插入 names.insert(索引,"xxxx")
>>> names = ["Eric","Tony","Jack","Rain","Tom","大魔王"] ["Eric","Tony","Jack","Rain","Tom","大魔王"] names.insert(2,"在Jack前面插入") >>> names ["Eric","Tony","在Jack前面插入","Jack","Rain","Tom","大魔王"] >>> names.insert(5,"在Rain后面插入") >>> names ["Eric","Tony","在Jack前面插入","Jack","Rain","在Rain后面插入","Tom","大魔王"]
修改 names[index] = "xxxxx"
>>> names ["Eric","Tony","在Jack前面插入","Jack","Rain","在Rain后面插入","Tom","大魔王"] >>> names[2] = "轮到我了" >>> names ["Eric","Tony","轮到我了","Jack","Rain","在Rain后面插入","Tom","大魔王"]
删除 del names[index] names.remove("xxxxx") names.pop()
>>> del names[2] >>> names [‘Eric‘, ‘Tony‘, ‘Jack‘, ‘Rain‘, ‘从Rain后面插入‘, ‘Tom‘, ‘大魔王‘ ] >>> del names[4] >>> names [‘Eric‘, ‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘, ‘大魔王‘] >>> >>> names.remove("Eric") #删除指定元素 >>> names [‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘, ‘大魔王‘] >>> names.pop() #删除列表最后一个值 ‘大魔王‘ >>> names [‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘]
扩展 names.extend(列表名)
>>> names [‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘] >>> b = [1,2,3] >>> names.extend(b) >>> names [‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘, 1, 2, 3]
拷贝 names.copy()
>>> names [‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘,1, 2, 3] >>> name_copy = names.copy() >>> name_copy [‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘,1, 2, 3]
统计 names.count("xxx")
>>> names [‘Tony‘, ‘Jack‘, ‘Rain‘, ‘Tom‘,1, 2, 3] >>> names.count("Amy") 1
排序 & 反转 names.sort() names.revserse()
>>> names [‘Alex‘, ‘Tenglan‘, ‘Amy‘, ‘Tom‘, ‘Amy‘, 1, 2, 3] >>> names.sort() #排序 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < str() #3.0里不同数据类型不能放在一起排序了,擦 >>> names[-3] = ‘1‘ >>> names[-2] = ‘2‘ >>> names[-1] = ‘3‘ >>> names [‘Alex‘, ‘Amy‘, ‘Amy‘, ‘Tenglan‘, ‘Tom‘, ‘1‘, ‘2‘, ‘3‘] >>> names.sort() >>> names [‘1‘, ‘2‘, ‘3‘, ‘Alex‘, ‘Amy‘, ‘Amy‘, ‘Tenglan‘, ‘Tom‘] >>> names.reverse() #反转 >>> names [‘Tom‘, ‘Tenglan‘, ‘Amy‘, ‘Amy‘, ‘Alex‘, ‘3‘, ‘2‘, ‘1‘]
获取下标 names.index("xxxxx")
>>> names [‘Tom‘, ‘Tenglan‘, ‘Amy‘, ‘Amy‘, ‘Alex‘, ‘3‘, ‘2‘, ‘1‘] >>> names.index("Amy") 2 #只返回找到的第一个下标
元祖:
创建元祖(不可修改)
names = ("Eric","Jack","Tom")
它只有2个方法,一个是count,一个是index.
字符串:
不可修改
name = "hello word!" name.title() #将字符串换成标题 Hello World! name.capitalize() #首字母大写 name.casefold() #大写全部变小写 name.center(50,"-") #输出‘------------hello word!------------’ name.count(‘ic‘) #统计ic出现次数 name.encode() #将字符串编码成bytes格式 name.endswith(‘ic‘) #判断字符串是否以ic结尾 name.find(‘ic‘) #f返回ic出现的位置 name.upper() #全部转换成大写 name.strip() #移除两边的空格 name.split() #分割字符串 name.replace(‘h‘,‘m‘,1) #把h替换成m,替换1次
字典:
字典是无序的,使用键-值(key-value)存储,具有极快的查找速度。
d = {‘Eric‘: 95, ‘Jack‘: 75, ‘Tom‘: 85} d.get(‘Jack‘) #根据key获取values,如果不存在返回None,这里输出75 d.pop(‘Tom‘) #根据键删除某一元素 d={‘Eric‘: 95, ‘jack‘: 75} d[‘Tony‘]=99 #新增元素 d={‘Eric‘: 95, ‘Jack‘: 75, ‘Tony‘: 99} print(len(d)) #输出字典长度,这里输出3 print(‘Eric‘ in d) #python3 中移除了 has_key,要判断键是否存在用in for i in d: print(i) #循环默认按键输出 for i in d.values(): #循环按值输出 print(i) for k,v in d.items(): #循环按键值输出 print(k,v)
枚举:
为一个可迭代的对象添加序号,可迭代的对象你可以理解成能用for循环的就是可迭代的。默认是编号是从0开始,可以设置从1开始
li = ["手机", "电脑", ‘鼠标垫‘, ‘游艇‘] for k, i in enumerate(li,1): print(k,i) 1 手机 2 电脑 3 鼠标垫 4 游艇
range和xrange:
在python2中有xrange和range,其中range会一次在内存中开辟出了所需的所有资源,而xrange则是在for循环中循环一次则开辟一次所需的内存,而在Python3中没有xrange,只有range ,但是python3的range代表的就是xrange。range用来指定范围,生成指定的数字。
for i in range(10): #循环输出所生成的 0-9 print(i) for i in range(1,10,2): #输出所生成的 1 3 5 7 9 print(i)
练习作业:
元素分类:
有如下值集合 [11,22,33,44,55,66,77,88,99],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。即: {‘k1‘: 大于66的所有值, ‘k2‘: 小于66的所有值}
l = [11,22,33,44,55,66,77,88,99]
以上是关于Python学习笔记_Python基础二的主要内容,如果未能解决你的问题,请参考以下文章