PYTHON内置数据结构 之 DICT

DICT字典

  • 字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现
# 字典的创建
# 创建空字典1
d = {}
print(d)

# 创建空字典2
d = dict()
print(d)

# 创建有值的字典, 每一组数据用冒号隔开, 每一对键值对用逗号隔开
d = {"one":1, "two":2, "three":3}
print(d)

# 用dict创建有内容字典1
d = dict({"one":1, "two":2, "three":3})
print(d)

# 用dict创建有内容字典2
# 利用关键字参数
d = dict(one=1, two=2, three=3)
print(d)


# 
d = dict( [("one",1), ("two",2), ("three",3)])
print(d)

输出:
{}
{}
{‘one’: 1, ‘two’: 2, ‘three’: 3}
{‘one’: 1, ‘two’: 2, ‘three’: 3}
{‘one’: 1, ‘two’: 2, ‘three’: 3}
{‘one’: 1, ‘two’: 2, ‘three’: 3}

字典的特征

  • 字典是序列类型,但是是无序序列,所以没有分片和索引
  • 字典中的数据每个都有键值对组成,即kv对
    • key: 必须是可哈希的值,比如int,string,float,tuple, 但是,list,set,dict 不行
    • value: 任何值

字典常见操作

# 访问数据
d = {"one":1, "two":2, "three":3}
# 注意访问格式
# 中括号内是键值
print(d["one"])


d["one"] = "eins"
print(d)

# 删除某个操作
# 使用del操作
del d["one"]
print(d)

输出:
1
{‘one’: ‘eins’, ‘two’: 2, ‘three’: 3}
{‘two’: 2, ‘three’: 3}

# 成员检测, in, not in
# 成员检测检测的是key内容
d = {"one":1, "two":2, "three":3}

if 2 in d:
    print("value")
    
if "two" in d:
    print("key")
    
if ("two",2) in d:
    print("kv")

输出:
key

# 便利在python2 和 3 中区别比较大,代码不通用
# 按key来使用for循环
d = {"one":1, "two":2, "three":3}
# 使用for循环,直接按key值访问
for k in d:
    print(k,  d[k])
    
# 上述代码可以改写成如下
for k in d.keys():
    print(k,  d[k])
    
# 只访问字典的值
for v in d.values():
    print(v)
    
# 注意以下特殊用法
for k,v in d.items():
    print(k,'--',v)

输出:
one 1
two 2
three 3
one 1
two 2
three 3
1
2
3
one – 1
two – 2
three – 3

字典生成式

d = {"one":1, "two":2, "three":3}

# 常规字典生成式
dd = {k:v for k,v in d.items()}
print(dd)


# 加限制条件的字典生成式
dd = {k:v for k,v in d.items() if v % 2 == 0}
print(dd)

输出:
{‘one’: 1, ‘two’: 2, ‘three’: 3}
{‘two’: 2}

字典相关函数

  • len
  • min
  • max
  • dict
  • clear
  • items
  • keys
  • values
  • get
  • fromkeys
  • update
# 通用函数: len, max, min, dict
# str(字典): 返回字典的字符串格式
d = {"one":1, "two":2, "three":3}
print(str(d))

输出:
{‘one’: 1, ‘two’: 2, ‘three’: 3}

# clear: 清空字典
# items: 返回字典的键值对组成的元组格式

d = {"one":1, "two":2, "three":3}
i = d.items()
print(type(i))
print(i)

输出:
<class ‘dict_items’>
dict_items([(‘one’, 1), (‘two’, 2), (‘three’, 3)])

# keys:返回字典的键组成的一个结构
k = d.keys()
print(type(k))
print(k)

输出:
<class ‘dict_keys’>
dict_keys([‘one’, ‘two’, ‘three’])

# values: 同理,一个可迭代的结构
v = d.values()
print(type(v))
print(v)

输出:
<class ‘dict_values’>
dict_values([1, 2, 3])

# get: 根据制定键返回相应的值, 好处是,可以设置默认值

d = {"one":1, "two":2, "three":3}
print(d.get("on333"))

# get默认值是None,可以设置
print(d.get("one", 100))
print(d.get("one333", 100))

#体会以下代码跟上面代码的区别
print(d['on333'])

输出:
None
1
100

KeyError Traceback (most recent call last)
in ()
9
10 #体会以下代码跟上面代码的区别
—> 11 print(d[‘on333’])

KeyError: ‘on333’

# fromkeys: 使用指定的序列作为键,使用一个值作为字典的所有的键的值
l = ["eins", "zwei", "drei"]
# 注意fromkeys两个参数的类型
# 注意fromkeys的调用主体
d = dict.fromkeys(l, "hahahahahah")
print(d)

输出:

{‘eins’: ‘hahahahahah’, ‘zwei’: ‘hahahahahah’, ‘drei’: ‘hahahahahah’}

DICT 添加元素

DICT是不可变的,如果添加元素,那么就是创建一个新的dict

  • 字典名[keys] = values
  • update
>>> d = {'a': 1}
>>> d.update(b=2)
>>> d
{'a': 1, 'b': 2}
>>> d.update(c=3, d=4)
>>> d
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
 
>>> d['e'] = 5
>>> d
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
 
>>> d.update({'f': 6, 'g': 7})
>>> d
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值