python字典、列表函数

  • Post author:
  • Post category:python




列表

print(dir(list))
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']



append()


  • 描述

    • 添加到列表最后

  • 语法

    • list.append()
test=[1, 2, 3, 4]
test.append({"new": "add"})
print(test)
[1, 2, 3, 4, {'new': 'add'}]



clear()


  • 描述

    • 清空列表

  • 语法

    • list.clear()
test=[1, 2, 3, 4]
test.clear()
print(test)
[]



copy()


  • 描述

    • 浅复制

  • 语法

    • list.copy()
test=[1, {"a": {"b": [2, 3]}}]
test_copy=test.copy()

print(test_copy)
test=[1, {"a": {"b": [2, 3]}}]  #成功复制

#修改
test_copy[1]["b"]=[5,5]

print(test)
[1, {'a': {'b': [2, 3]}, 'b': [5, 5]}]
#当复制列表里面第二层是列表或字典时,对副本的操作会影响到原列表

#可以使用copy模块的copy.deepcopy()完成深拷贝



count()


  • 描述

    • 查询列表中某个值的次数

  • 语法

    • list.count(value)
test=[1, 1, 12, {"a": 1}]

#查看1的次数
test.count(1)
2

#查看
test.count({"a": 1})
1



extend()

  • 描述

    • 将列表插入到列表末尾
  • 语法

    • list.extend(list)
  • 参数

    • list ====> 列表
test=[1, 2, 3, 4]
test_ex=[5,6,7]
test.extend(test_ex)  #插入

print(test) #查看
[1, 2, 3, 4, 5, 6, 7]



index()

  • 描述

    • 查看某个值得下标
  • 语法

    • list.index(value, [start, [stop]])
  • 参数

    • value ====> 查找的值
    • start stop 起始终止位置
test = [1, 2, 3, 4, 5, 6, 7]

test.index(5)
4   #返回下标

#从1下标开始查找到2下标 查找值为7的下标
test.index(7,1,2)
ValueError: 7 is not in list




insert()


  • 描述

    • 在下标前插入

  • 语法

    • list.insert(index,object)

  • 参数

    • index ====> 下标
    • object ====> 插入的对象
test = [1, 2, 3, 4, 5]
test.insert(3, 6)

#查看
test
[1, 2, 3, 6, 4, 5]
#6被插入到3下标位置



pop()和remove()

#pop(index)  默认是最后
test = [1, 2, 3, 4, 5]
test.pop()
5

test
[1, 2, 3, 4]

#remove(value)   移除第一个符合的值
test.remove(3)
[1, 2, 4]



reverse()


  • 描述

    • 反转列表

  • 语法

    • list.reverse()
test = [1, 2, 3, 4]
test.reverse()

#查看
test
[4, 3, 2, 1]



sort()


  • 描述

    • 排序

  • 语法

    • list.sort(cmp=None, key=None, reverse=False)

  • 参数

    • cmp ====> 可选参数,如果指定了该参数会使用该参数的方法进行排序
    • key ====> 主要是用来进行比较的元素
    • reverse ====> 排序规则 reverse = True

      降序

      reverse = False

      升序

      (默认)




字典

print(dir(dict))
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']



clear()


  • 描述

    • 清除字典中所有元素,返回值None

  • 语法

    • dict.clear()
test={"i":"study"}
print(test.clear())
None                    #返回值None
print(test)
{}						#字典被清空了  clear影响字典本身



copy()


  • 描述

    • 返回一个字典的拷贝副本(浅)

  • 语法

    • dict.copy()
test={"i": "study", "first": {"second": [1, 2, 3]}}
test_copy=test.copy()
print(test_copy)
{"i": "study", "first": {"second": [1, 2, 3]}}  #拷贝成功

#修改拷贝的字典中第二层
test_copy["first"]["second"] = [4, 5, 6]
#查看被拷贝的字典
print(test)
{'i': 'study', 'first': {'second': [4, 5, 6]}}
#                                  被拷贝字典的二层字典的值被修改了 说明是浅拷贝


tip 深拷贝使用copy模块

import copy
test={"i": "study", "first": {"second": [1, 2, 3]}}
test_copy=copy.deepcopy(test)
print(test_copy)
{"i": "study", "first": {"second": [1, 2, 3]}}  #拷贝成功

#修改拷贝的字典中第二层
test_copy["first"]["second"] = [4, 5, 6]
#查看被拷贝的字典
print(test_copy,test)
{'i': 'study', 'first': {'second': [4, 5, 6]}} {'i': 'study', 'first': {'second': [1, 2, 3]}}
#test的first字典并没有被修改



fromkeys()


  • 描述

    • 创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值

  • 语法

    • dict.fromkeys(seq[,value])

  • 参数

    • seq ====> 字典键值列表
    • value ====> 可选参数,设置键序列(seq)的值
seq=(1,2,3,4)
value=[5,6]
dict.fromkeys(seq)  #没有指定value值   默认为None
{1: None, 2: None, 3: None, 4: None}

dict.fromkeys(seq,value)  #指定为列表
{1: [5, 6], 2: [5, 6], 3: [5, 6], 4: [5, 6]}



get()


  • 描述

    • 返回指点键的值,如果值不在字典中返回default(None)值

  • 语法

    • dict.get(key,default=None)

  • 参数

    • key ====> 字典中要查找的键
    • default ====> 如果指定键的值不存在时,返回的默认值
test={"a": "b", "c": "d"}
test.get("a")
'b' #找到返回值
test.get("e","Null")
'Null'   #找不到  返回默认值



items()


  • 描述

    • 列表返回遍历的键值对元组数组

  • 语法

    • dict.items()
test={"i": "study", "first": {"second": [1, 2, 3]}}
test_items=test.items()

print(type(test_items))
<class 'dict_items'>

print(test_items)
dict_items([('i', 'study'), ('first', {'second': [1, 2, 3]})])
#列表元组



keys()


  • 描述

    • 以列表返回一个字典所有的键

  • 语法

    • dict.keys()
test={"i": "study", "first": {"second": [1, 2, 3]}}
test.keys()
dict_keys(['i', 'first'])



pop()


  • 描述

    • 删除对应键

  • 语法

    • pop(key[,default])

  • 参数

    • key ====> 要删除的键值
    • default ====> 如果没有key,返回default
test={"i": "study", "first": {"second": [1, 2, 3]}}
test.pop("i")  
'study'         #成功找到i键的值

test.pop("i","not found")
'not found'        #已经被删除了 所以输出默认返回值

test.pop("i")
KeyError: 'i'     #已经被删除了 没有默认返回 返回KeyError



popitem()


  • 描述

    • 删除字典中某一键值对,并以元组的形式返回这一键值对,返回并删除字典中的最后一对键和值

  • 语法

    • dict.popitem()
test={"i": "study", "first": {"second": [1, 2, 3]}}
test.popitem()         #删除最后一对键值对  根据自己定义的顺序
('first', {'second': [1, 2, 3]})

print(test)  #查看剩下啥
{'i': 'study'}  

test.popitem()
('i', 'study')

test.popitem()
KeyError: 'popitem(): dictionary is empty'



setdefault()


  • 描述

    • 如果键不存在于字典中,就会添加键并将值设置为默认值

  • 语法

    • dict.setdefault(key,default=None)

  • 参数

    • key ====> 查找的键值
    • default ====> 键不存在时,设置的默认键值
test={"i": "study", "first": {"second": [1, 2, 3]}}

#存在时,返回该键的值
test.setdefault("i")
'study'

#不存在时,会把默认值赋值
test.setdefault("a","new") 
'new'
print(test)
{'i': 'study', 'first': {'second': [1, 2, 3]}, 'a': 'new'}

#已经存在时,默认值不会被赋值
test.setdefault("a","old")
'new'         #返回原本的值
print(test) 
{'i': 'study', 'first': {'second': [1, 2, 3]}, 'a': 'new'}
#键a的值仍然是new



update()


  • 描述

    • 更新字典的键值对,将参数中字典中的键值对更新到字典中,此方法无返回值

  • 语法

    • dict.update(dict)

  • 参数

    • dict ====> 添加到指定字典dict里的字典
test={"i": "study", "first": {"second": [1, 2, 3]}}
test.update({"i":"new"})
print(test)
{'i': 'new', 'first': {'second': [1, 2, 3]}}



values()


  • 描述

    • 返回字典中所有键对应的值

  • 语法

    • dict.values()
test={"i": "study", "first": {"second": [1, 2, 3]}}
test.values()
#返回值
dict_values(['study', {'second': [1, 2, 3]}])



版权声明:本文为qq_44692240原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。