python字典操作

文章目录

字典的定义方式

a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
f = dict({'one': 1, 'three': 3}, two=2)
print(a == b == c == d == e == f)

字典的操作

1、 list(d) 返回字典 d 中使用的所有键的列表。
a = dict(one=1, two=2, three=3)
print(list(a))
['one', 'two', 'three']
2、 len(d)返回字典 d 中的项数。
a = dict(one=1, two=2, three=3)
print(len(a))
3
3、 d[key]返回 d 中以 key 为键的项。 如果映射中不存在 key 则会引发 KeyError。
a = dict(one=1, two=2, three=3)
print(a["two"])
2
print(a["twoo"])
KeyError: 'twoo'
4、 d[key] = valued[key] 设为 value
d = {'one': 1, 'two': 2, 'three': 3}
print(d['one'])
1
d['one'] = 10
print(d['one'])
10
5、 del d[key]将 d[key]d 中移除。如果映射中不存在 key 则会引发 KeyError。
d = {'one': 1, 'two': 2, 'three': 3}
print(d)
del d['one']
print(d)

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

del d['onee']
KeyError: 'onee'
6、 key in d 如果 d 中存在键 key 则返回 True,否则返回 False
d = {'one': 1, 'two': 2, 'three': 3}
print('one' in d)
print('five' in d)

True
False
7、 key not in d等价于 not key in d
d = {'one': 1, 'two': 2, 'three': 3}
print('five' not in d)
print('one' not in d)

True
False
8、 iter(d)返回以字典的键为元素的迭代器。 这是 iter(d.keys()) 的快捷方式。
d = {'one': 1, 'two': 2, 'three': 3}

for i in iter(d):
    print(i)
    
one
two
three
9、 clear()移除字典中的所有元素。
d = {'one': 1, 'two': 2, 'three': 3}
d.clear()
print(d)

{}
10、 copy()返回原字典的浅拷贝。
d = {'one': 1, 'two': 2, 'three': 3}
print(d.copy())
print(id(d))
print(id(d.copy()))

{'one': 1, 'two': 2, 'three': 3}
2118555895488 
2118555895680  # 浅拷贝不指向同一片内存地址
11、 classmethod fromkeys(iterable[, value])使用来自 iterable 的键创建一个新字典,并将键值设为 value。返回新字典的类方法。 value 默认为 None
d = {}
d = d.fromkeys(range(3),list(range(3)))
print(d)

{0: [0, 1, 2], 1: [0, 1, 2], 2: [0, 1, 2]}
12、 get(key[, default])如果 key 存在于字典中则返回 key 的值,否则返回 default。 如果 default 未给出则默认为 None,因而此方法绝不会引发 KeyError。
d = {'one': 1, 'two': 2, 'three': 3}

print(d.get('one'))
print(d.get('onee'))
print(d.get('onee'),10)

1
None
None 10
13、 items()返回由字典项 ((键, 值) 对) 组成的一个新视图。
d = {'one': 1, 'two': 2, 'three': 3}

for k,v in d.items():
    print(k,v)
    
one 1
two 2
three 3
14、 keys()返回由字典键组成的一个新视图。
d = {'one': 1, 'two': 2, 'three': 3}
for k in d.keys():
    print(k)
one
two
three
15、 pop(key[, default])如果 key 存在于字典中则将其移除并返回其值,否则返回 default。 如果 default 未给出且 key 不存在于字典中,则会引发 KeyError。
d = {'one': 1, 'two': 2, 'three': 3}

d.pop('one')
print(d)
{'two': 2, 'three': 3}

16、 popitem() 从字典中移除并返回一个 (键, 值) 对。 键值对会按 LIFO (后进先出)的顺序被返回。
d = {'one': 1, 'two': 2, 'three': 3}

d.popitem()
print(d)
{'one': 1, 'two': 2}
17、 reversed(d) 返回一个逆序获取字典键的迭代器。 这是 reversed(d.keys()) 的快捷方式。
d = {'one': 1, 'two': 2, 'three': 3}

for i in reversed(d):
    print(i)
    
three
two
one
18、 setdefault(key[, default])如果字典存在键 key ,返回它的值。如果不存在,插入值为 default 的键 key ,并返回 defaultdefault 默认为 None
d = {'one': 1, 'two': 2, 'three': 3}

print(d.setdefault('one'))
print(d.setdefault('onee'))
print(d.setdefault('onee'),100)

1
None
None 100
19、 update([other])使用来自 other 的键/值对更新字典,覆盖原有的键。 返回 None
d = {'one': 1, 'two': 2, 'three': 3}
d1 = {'one': 10, 'name': 'jason'}
d.update(d1)
print(d)

{'one': 10, 'two': 2, 'three': 3, 'name': 'jason'}
20、 values() 返回由字典值组成的一个新视图。
d = {'one': 1, 'two': 2, 'three': 3}
print(d.values())
dict_values([1, 2, 3])

for i in d.values():
    print(i)
1
2
3
21、 d | other 合并 dother 中的键和值来创建一个新的字典,两者必须都是字典。当 dother 有相同键时, other 的值优先。
d = {'one': 1, 'two': 2, 'three': 3}
d1 = {'one': 10, 'two': 20, 'three': 30}
print(d | d1)
{'one': 10, 'two': 20, 'three': 30}


d = {'one': 1, 'two': 2, 'three': 3}
d1 = {'name': 'jason', 'age': 18}
print(d | d1)
{'one': 1, 'two': 2, 'three': 3, 'name': 'jason', 'age': 18}

22、 d |= otherother 的键和值更新字典 dother 可以是 mappingiterable 的键值对。当 dother 有相同键时, other 的值优先。
d = {'one': 1, 'two': 2, 'three': 3}
d1 = {'name': 'jason', 'age': 18}
d |= d1
print(d)
{'one': 1, 'two': 2, 'three': 3, 'name': 'jason', 'age': 18}


d = {'one': 1, 'two': 2, 'three': 3}
d1 = {'one': 10, 'two': 20, 'three': 30}
d |= d1
print(d)
{'one': 10, 'two': 20, 'three': 30}

# mapping
# iterable
1 = {'name': 'jason', 'age': 18}
d |= d1
print(d)
{'one': 1, 'two': 2, 'three': 3, 'name': 'jason', 'age': 18}


d = {'one': 1, 'two': 2, 'three': 3}
d1 = {'one': 10, 'two': 20, 'three': 30}
d |= d1
print(d)
{'one': 10, 'two': 20, 'three': 30}

# mapping
# iterable
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值