Attic»论坛 Python Base 字典

字典

[复制链接]
发表于 2025-9-23 16:34:42 | 显示全部楼层 |阅读模式
#字典
testdict = {'a':1,'b':2,'c':3,'d':{'e':3}}
print(testdict)
print(testdict['a'])
print(testdict['d']['e'])
#有键名相同时默认取最后一个
#字典
testdict = {'a':1,'b':2,'c':3,'d':{'e':3}}
print(testdict)
print(testdict['a'])
print(testdict['d']['e'])
#有键名相同时默认取最后一个
#新增
testdict['f'] = 12
print(testdict)
#修改
testdict['a'] = 123
print(testdict)
#修改
testdict.update({'a':123,'b':456})
print(testdict)
#删除 del pop clear popitem
p = testdict.pop('a')
print(testdict)
print(p)
del testdict['b']
print(testdict)
testdict.clear()
print(testdict)

{'a': 1, 'b': 2, 'c': 3, 'd': {'e': 3}}
1
3
{'a': 1, 'b': 2, 'c': 3, 'd': {'e': 3}, 'f': 12}
{'a': 123, 'b': 2, 'c': 3, 'd': {'e': 3}, 'f': 12}
{'a': 123, 'b': 456, 'c': 3, 'd': {'e': 3}, 'f': 12}
{'b': 456, 'c': 3, 'd': {'e': 3}, 'f': 12}
123
{'c': 3, 'd': {'e': 3}, 'f': 12}
{}

#查询
if 'd' in  testdict:
    print('d在字典中')
else:
    print('d不在字典中')

复制

testdict = {'a':1,'b':2,'c':3,'d':{'e':3}}
testdict.pop();

testcopy=testdic.copy()
print(testcopy)

GMT+8, 2025-10-17 02:43 , Processed in 0.091251 second(s), 34 queries Archiver|手机版|小黑屋|Attic ( 京ICP备2020048627号 )

快速回复 返回顶部 返回列表