标签:字典元素 class error ble isp one 不可 列表 controls
#字典创建
>>> dict2 = { ‘abc‘: 123, 98.6: 37 }
>>> dict2[98.6]
37
>>> dict2["abc"]
123
键必须不可变,所以可以用数字,字符串或元组充当,用列表就不行
#!/usr/bin/python
dict = {[‘Name‘]: ‘Zara‘, ‘Age‘: 7}
print "dict[‘Name‘]: ", dict[‘Name‘]
Traceback (most recent call last):
File "test.py", line 3, in <module>
dict = {[‘Name‘]: ‘Zara‘, ‘Age‘: 7}
TypeError: list objects are unhashable
#字典元素删除
#!/usr/bin/python
# -*- coding: UTF-8 -*-
dict = {‘Name‘: ‘Zara‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
del dict[‘Name‘] # 删除键是‘Name‘的条目
dict.clear() # 清空字典所有条目
del dict # 删除字典
print "dict[‘Age‘]: ", dict[‘Age‘]
print "dict[‘School‘]: ", dict[‘School‘]
#键一般是唯一的,如果重复最后的一个键值对会替换前面的,值不需要唯一。
>>>dict = {‘a‘: 1, ‘b‘: 2, ‘b‘: ‘3‘}
>>> dict[‘b‘]
‘3‘
>>> dict
{‘a‘: 1, ‘b‘: ‘3‘}
标签:字典元素 class error ble isp one 不可 列表 controls
原文地址:https://www.cnblogs.com/sea-stream/p/11167440.html