Python中的字典数据类型
Python的字典是一种哈希表类型。它们的工作方式类似于Perl中的关联数组或哈希,并且由键值对组成。字典键几乎可以是任何Python类型,但通常是数字或字符串。另一方面,值可以是任意Python对象。
示例
字典用花括号({})括起来,可以使用方括号([])分配和访问值。例如-
#!/usr/bin/python
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values输出结果
这产生以下结果-
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']字典没有元素间顺序的概念。说元素“乱序”是不正确的。他们只是无序的。
热门推荐
10 五二零朋友祝福语大全简短
11 新婚祝福语大全简短经典
12 老师节祝福语的简短
13 公司文案生日祝福语简短
14 新年祝同学祝福语简短
15 满月红包英文祝福语简短
16 项目建设春节祝福语简短
17 生日祝福语贺卡老师简短
18 新年祝福语大全简短老师