1.4数据类型(dict)

字典创建

字典的键值对用冒号分割,每对之间用逗号分割,整个字典用花括号中,键值唯一,不可变,可以为字符串,数字或元祖。例如:

>>> first_dict = {"abc":456,456:"hello",("love","python"):"loving"}

字典访问

将相应的键放入方括号里作为索引,例如

>>> first_dict = {"abc":456,456:"hello",("love","python"):"loving"}

>>> first_dict["abc"]

456

字典修改

可以向字典添加新的键值对,修改键值,例如:

>>> first_dict = {"abc":456,456:"hello",("love","python"):"loving"}

>>>first_dict
{456: 'hello', 'abc': 456, ('love', 'python'): 'loving'}

>>> first_dict["abc"] = 123  #修改
>>> first_dict
{456: 'hello', 'abc': 123, ('love', 'python'): 'loving'}

>>> first_dict["first"] = "time" #添加
>>> first_dict
{456: 'hello', 'abc': 123, 'first': 'time', ('love', 'python'): 'loving'}

字典删除

删除单一元素、清空字典、删除整个字典,例如:

>>> test_dict = {"name" : "yangyang","age":26,"class":"first"}
>>> del test_dict["class"]   #删除单一元素
>>> test_dict
{'age': 26, 'name': 'yangyang'}

>>> first_dict.clear()  #清空字典
>>> first_dict
{}
>>> del test_dict  #删除整个字典
>>> test_dict

Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
test_dict
NameError: name 'test_dict' is not defined

字典函数

语法 描述 参数 返回 实例
cmp(dict1,dict2) 比较两个字典元素

dict1--比较字典

dict2--比较字典

dict1 > dict2:1

dict1 < dict2:-1

dict1 == dict2:0

>>> dict1 = {"name" : "Zara","age" : 7}
>>> dict2 = {"name" : "Mahnaz","age" : 27}
>>> dict3 = {"name" : "Abid","age" : 27}
>>> dict4 = {"name" : "Zara","age" : 7}
>>> cmp(dict1,dict2)
-1
>>> cmp(dict2,dict3)
1
>>> cmp(dict1,dict4)
0

len(dict) 计算字典元素个数,即键总和 dict--需计算元素个数的字典 返回字典的元素个数

>>> dict1 = {"name":"lulu","age":7}
>>> dict1 = {"name" : "Zara","age" : 7}
>>> len(dict1)
2

str(dict) 输出字典可打印的字符串表示 dict--需要转换成字符串的字典 返回字典被转换后的字典

>>> dict1 = {"name" : "Zara","age" : 7}
>>> str(dict1)
"{'age': 7, 'name': 'Zara'}"

 字典方法

语法 描述 参数 返回 实例
dict.clear() 清除字典内所有元素

>>> test_dict
{'age': 27, 'name': 'yangyang'}
>>> test_dict.clear()
>>> test_dict
{}

dict.get(key,default=None) 返回指定键的值

key--字典中要查找的键

返回指定键的值

>>> test_dict = {"name" : "yangyang","age" : 27}
>>> test_dict.get("name")
'yangyang'

dict.has_key(key) 如果键在字典dict中返回true,否则则返回false key--字典中要查找的键 如果键在字典dict中返回true,否则则返回false

>>> test_dict = {"name" : "yangyang","age" : 27}
>>> test_dict.has_key("name")
True
>>> test_dict.has_key("class")
False

dict.items() 以列表的返回可遍历的(键,值)元祖数组 可遍历的(键,值)元祖数组

>>> test_dict = {"name" : "yangyang","age" : 27}
>>> test_dict.items()

[('age', 27), ('name', 'yangyang')]
dict.keys() 以列表形式返回所有的键值 以列表形式返回所有的键值

>>> test_dict = {"name" : "yangyang","age" : 27}
>>> test_dict.keys()
['age', 'name']

dict.update(dict2) 将dict2中值更新到dict dict2--添加到指定dict里的字典 无返回值

>>>test_dict = {"name" : "yangyang","age" : 27}

>>> update_dict = {"class" : "first"}

>>> test_dict.update(update_dic)
>>> test_dict
{'age': 27, 'name': 'yangyang', 'class': 'first'}

dict.values() 返回字典中所有值 返回字典中所有值

>>> test_dict = {"name" : "yangyang","age" : 27}

>>> test_dict.values()
[27, 'yangyang']

pop() 删除字典中的一对键值 返回一个键值对(key,value)

>>> test_dict = {"name" : "yangyang","age" : 27,"class" : "first"}
>>> test_dict.popitem()
('age', 27)

你可能感兴趣的