Python编程:容器总结-字符串、列表、元组、字典、集合

操作 string list tuple dict set
@== 增 ==
创建 “” list()[] tuple()(,) dict()、fromkeys(){key: value} set(){}
追加 append() add()
插入 insert()
扩展 extend() update() update()
@== 删 ==
移除 remove() pop(key[, default]) remove()会报错discard()不报错
删除 del del
随机弹出 pop() popitem() pop()
清空 clear() clear()
@== 改 ==
修改 lst[index] = value dict[key] = value
设置默认 setdefault()
@== 查 ==
获取元素
查找 get(key,default=None)
切片 [start: end: step]
查找索引 index()
遍历 for value in list for key in dict
成员检查 has_key(key)
键值元组 items()
keys()
values()
@== 其他 ==
计数 count()
反转 reverse()
排序 sort()
浅拷贝 copy() copy()
成员检查 in in in key in dict in

参考

  1. Python全栈之路:list列表常用方法
  2. Python全栈之路:字典dict常用方法
  3. Python全栈之路:集合set常用方法
  4. Python全栈之路:string字符串常用方法

猜你喜欢

转载自blog.csdn.net/mouday/article/details/82586829
今日推荐