###list类的方法
###append 列表内最后增加一个元素
a = [1,2,3,4,5,6,"dssdsd"]
a.append(5)
print(a)
###clear 清空列表内的元素
示例:
a = [1,2,3,4,5,6,"dssdsd"]
a.clear()
print(a)
###copy 复制列表
示例:
a = [1,2,3,4,5,6,"dssdsd"]
v1 = a.copy()
print(v1)
###count 列表内有几个指定的元素个数
示例:
a = [1,2,3,4,5,6,"dssdsd",2]
v1 = a.count(2)
print(v1)
###extend 在列表内最后增加元素
示例:
a = [1,2,3,4,5,6,"dssdsd",2]
v1 = a.extend("4")
print(a)
###index 列表内元素的索引位置
示例:
a = [1,2,3,4,5,6,"dssdsd",2]
v1 = a.index(3)
print(v1)
###index 插入索引插入元素到表内
示例:
a = [1,2,3,4,5,6,"dssdsd",2]
a.insert(2,"index")
print(a)
###pop 利用索引删除指定列表内的元素
示例:
a = [1,2,3,4,5,6,"dssdsd",2]
a.pop(0)
print(a)
###remove 删除列表内指定元素
示例:
a = [1,2,3,4,5,6,"dssdsd",2]
a.remove(1)
print(a)
###reverse 反转列表
示例:
a = [1,2,3,4,5,6,"dssdsd",2]
a.reverse()
print(a)
###sort 列表内从小到大排序 注意只能对数字列表做从小到大排序。
示例:
a = [13,12,23,43,55,6,4545,2]
a.sort()
print(a)
##########dict字典方法
###fromkeys 根据序列创建字典,并指定统一的值
示例:
v = dict.fromkeys(["asd",234,3333,2323],555)
print(v)
###get 打印出字典中key的值
示例:
info = {"k1":3,"k2":4,"k3":5,"k4":6,"k5":7}
v = info.get("k1") ###get("asd",1111)当一个key asd在字典中不存在时,输出后面的1111
print(v)
###pop 删除字典中一个键值对
示例:
info = {"k1":3,"k2":4,"k3":5,"k4":6,"k5":7}
v = info.pop("k1")
print(info,v)
###popitem 随机删除字典中的一个键值对
示例:
info = {"k1":3,"k2":4,"k3":5,"k4":6,"k5":7}
v = info.popitem()
print(info,v)
###setdefault 设置一个值,当这个key在字典中存在时,获取设置key的值,当设置值不存在时增加到字典中
示例:
info = {"k1":3,"k2":4,"k3":5,"k4":6,"k5":7}
v = info.popitem()
print(info,v)
###update 更新字典中的值,当键值对不存在时,增加到字典中
示例:
info = {"k1":3,"k2":4,"k3":5,"k4":6,"k5":7}
v = info.update({"k1":1000,"k7":4000})
print(info)
###keys 获取字典中所有key的值
示例:
info = {"k1":3,"k2":4,"k3":5,"k4":6,"k5":7}
v = info.keys()
print(v)
###values 获取字典中所有values的值
示例:
info = {"k1":3,"k2":4,"k3":5,"k4":6,"k5":7}
v = info.values()
print(v)
Python list和dict方法
Posted eddycomeon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python list和dict方法相关的知识,希望对你有一定的参考价值。
以上是关于Python list和dict方法的主要内容,如果未能解决你的问题,请参考以下文章
Python基础-list,tuple,dict,set常用方法及区别
Python 学习笔记 0308 周二:tuple,list,dict的区别