python3字典详解_python3中字典详解

字典(dict)

1. 创建字典的几种方式

class dict(**kwarg)

class dict(mapping, **kwarg)

class dict(iterable, **kwarg)

使用上面的方法构建字典`{"one:1", "two":2, "three":3}

方法1构建字典`:

a = dict(one=1, two=2, three=3)

a

输出结果:

{'one': 1, 'three': 3, 'two': 2}

方法2构建字典:

a = dict({'one': 1, 'two': 2, 'three': 3})

a

输出结果:

{'one': 1, 'three': 3, 'two': 2}

方法3构建字典:

d = dict([('two', 2), ('one', 1), ('three', 3)])

print('d =', d)

e = dict(zip(['one', 'two', 'three'], [1, 2, 3]))

print('e =', e)

输出结果:

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

e = {'one': 1, 'three': 3, 'two': 2}

方法4构建字典:

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

d

输出结果:

{'one': 1, 'three': 3, 'two': 2}

方法5构建字典:

# 创建一个空字典

a = dict()

# 通过赋值语句构造字典

a['one'] = 1

a['two'] = 2

a['three'] = 3

a

输出结果:

{'one': 1, 'three': 3, 'two': 2}

2. 对字典可以运用的一些操作

len(d): 返回字典的长度

a = dict(one=1, two=2, three=3)

len(a)

输出结果:

3

d[key1]:返回字典中key等于key1的值

a = dict(one=1, two=2, three=3)

a['one']

输出结果:

1

del d[key]:删除字典中key为key的值

a = dict(one=1, two=2, three=3)

del a['two']

a

输出结果:

{'one': 1, 'three': 3}

key in d 或者 key not in d:判断字典中有没有key为key的值

a = dict(one=1, two=2, three=3)

print('one' in a)

print('four' in a)

输出结果:

True

False

iter(d):返回字典中key的迭代器

a = dict(one=1, two=2, three=3)

d_iter = iter(a)

[x for x in d_iter]

输出结果:

['one', 'three', 'two']

3. 字典中的方法

dic.clear():将字典清空

a = dict(one=1, two=2, three=3)

a.clear()

print(a)

输出结果:

{}

dic.copy():浅复制复制一个字典。浅拷贝不会拷贝子对象,所以原始数据改变,子对象也会改变

a = dict(one=1, two=2, three=3)

b = a.copy()

print('a =', a)

print('b =', b)

# 更改b中的值

b['four'] = 4

print('updated:')

print('a =', a)

print('b =', b)

# 另外一种情况

print()

x = {"a": 123, "b": 444, "c": [1, 2, 6, "asd"]}

y = x.copy()

y["c"].remove("asd")

y["a"] = "xxx"

print('x=', x)

print('y=', y)

输出结果:

a = {'one': 1, 'three': 3, 'two': 2}

b = {'one': 1, 'three': 3, 'two': 2}

updated:

a = {'one': 1, 'three': 3, 'two': 2}

b = {'one': 1, 'three': 3, 'two': 2, 'four': 4}

x= {'a': 123, 'c': [1, 2, 6], 'b': 444}

y= {'a': 'xxx', 'c': [1, 2, 6], 'b': 444}

dic.items():返回字典中(key,value)对的迭代器

a = dict(one=1, two=2, three=3)

print(a.items())

输出结果:

dict_items([('one', 1), ('three', 3), ('two', 2)])

dic.keys():返回字典中key的迭代器

a = dict(one=1, two=2, three=3)

print(a.keys())

输出结果:

dict_keys(['one', 'three', 'two'])

dic.values():返回字典中值的迭代器

a = dict(one=1, two=2, three=3)

print(a.values())

输出结果:

dict_values([1, 3, 2])

dic.update([other]):更新字典

a = dict(one=1, two=2, three=3)

# 使用参数

a.update(four=4)

print(a)

# 使用字典来更新

other = {'five': 5, 'six': 6}

a.update(other)

print(a)

# 使用迭代器

a.update([('seven', 7),('eight', 8)])

print(a)

# 注意上面使用字典和迭代器来更新字典时,需要增加的字典和迭代器的长度大于2,否则会出现错误

输出结果:

{'one': 1, 'three': 3, 'two': 2, 'four': 4}

{'one': 1, 'three': 3, 'six': 6, 'five': 5, 'two': 2, 'four': 4}

{'one': 1, 'seven': 7, 'three': 3, 'eight': 8, 'six': 6, 'five': 5, 'two': 2, 'four': 4}

dic.popitem():随机删除一项,并返回键值对

a = dict(one=1, two=2, three=3)

print(a.popitem())

print('a =', a)

输出结果:

('one', 1)

a = {'three': 3, 'two': 2}

dic.pop(key[,default):删除并返回给定键的值,并删除键值对

a = dict(one=1, two=2, three=3)

print(a.pop('one'))

print(a.pop('four', 4))

输出结果:

1

4

dic.get(key[,default]):返回给定key的值,如果字典中没有key,则返回default值

a = dict(one=1, two=2, three=3)

print(a.get('two'))

print(a.get('four'), 3)

输出结果:

2

None 3

dic.setdefault(key[,default]):和dic.get()类似,如果没有key,则将{key:default}添加到字典中

a = dict(one=1, two=2, three=3)

print(a.setdefault('two'))

print(a.setdefault('four', 4))

print(a)

输出结果:

2

4

{'one': 1, 'three': 3, 'two': 2, 'four': 4}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值