VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • 深入探究Python中的字典容器(3)

 

关于 dict 类的帮助指出,可以使用构造函数直接创建字典(dictionary),而不使用花括号。既然与其他容器数据类型相比,在创建字典(dictionary)时必须提供更多的数据,那么这些创建方法比较复杂也就不足为奇了。但是,在实践中使用字典(dictionary)并不难,如清单 3 所示。

清单 3. 在 Python 中创建字典(dictionary),第 2 部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> l = [01,2 3456789
>>> d = dict(l)(most recent call last):
 File "<stdin>", line 1in ?: can't convert dictionary 
 update sequence element #0 to a sequence
  
>>> l = [(0'zero'), (1'one'), (2'two'), (3'three')]
>>> d = dict(l)
>>> d
{0'zero'1'one'2'two'3'three'}
>>> l = [[0'zero'], [1'one'], [2'two'], [3'three']]
>>> d
{0'zero'1'one'2'two'3'three'}
>>> d = dict(l)
>>> d
{0'zero'1'one'2'two'3'three'}
>>> d = dict(zero=0, one=1, two=2, three=3
>>> d
{'zero'0'three'3'two'2'one'1}
>>> d = dict(0=zero, 1=one, 2=two, 3=three): keyword can't be an expression

相关教程