首页 > 程序开发 > Web开发 > Python >

Python中的字典索引

2013-07-12

Python中的符合数据类型:字符串,列表和序列。它们用整数作为索引。如果你试图用其他的类型做索引,就会产生错误。[python]>>> list = [1 ,2,3]>>> list[0]1>>> list['one']Traceback (most re...

Python中的符合数据类型:字符串,列表和序列。它们用整数作为索引。如果你试图用其他的类型做索引,就会产生错误。


[python]
>>> list = [1 ,2,3]
>>> list[0]
1
>>> list['one']
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
list[&#39;one&#39;]
TypeError: list indices must be integers, not str

>>> list = [1 ,2,3]
>>> list[0]
1
>>> list[&#39;one&#39;]
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
list[&#39;one&#39;]
TypeError: list indices must be integers, not str
字典的索引可以是字符串,除了这一点,它与其组合类型非常相似。当然,字典的索引也可以是整数。


[python]
>>> dict1 = {&#39;mother&#39;:&#39;妈妈&#39;,&#39;father&#39;,&#39;爸爸&#39;}
SyntaxError: invalid syntax
>>> dict1 = {&#39;mother&#39;:&#39;妈妈&#39;,&#39;father&#39;:&#39;爸爸&#39;}
>>> dict1
{&#39;father&#39;: &#39;爸爸&#39;, &#39;mother&#39;: &#39;妈妈&#39;}

>>> dict1 = {&#39;mother&#39;:&#39;妈妈&#39;,&#39;father&#39;,&#39;爸爸&#39;}
SyntaxError: invalid syntax
>>> dict1 = {&#39;mother&#39;:&#39;妈妈&#39;,&#39;father&#39;:&#39;爸爸&#39;}
>>> dict1
{&#39;father&#39;: &#39;爸爸&#39;, &#39;mother&#39;: &#39;妈妈&#39;}我们也可以创造一个空字典然后在添加元素。


[python]
>>> eng2sp = {}
>>> eng2sp[&#39;one&#39;] = &#39;uno&#39;
>>> eng2sp[&#39;two&#39;] = &#39;dos&#39;
>>> eng2sp
{&#39;one&#39;: &#39;uno&#39;, &#39;two&#39;: &#39;dos&#39;}

>>> eng2sp = {}
>>> eng2sp[&#39;one&#39;] = &#39;uno&#39;
>>> eng2sp[&#39;two&#39;] = &#39;dos&#39;
>>> eng2sp
{&#39;one&#39;: &#39;uno&#39;, &#39;two&#39;: &#39;dos&#39;}字典元素以逗号作为分隔符,每个元素包含键和键值,他们俩用冒号进行分割

字典的删除


[python]
>>> inventory = {&#39;apples&#39;:430,&#39;bananas&#39;:312,&#39;oranges&#39;:525}
>>> inventory
{&#39;bananas&#39;: 312, &#39;apples&#39;: 430, &#39;oranges&#39;: 525}
>>> del inventory[&#39;apples&#39;]
>>> inventory
{&#39;bananas&#39;: 312, &#39;oranges&#39;: 525}

>>> inventory = {&#39;apples&#39;:430,&#39;bananas&#39;:312,&#39;oranges&#39;:525}
>>> inventory
{&#39;bananas&#39;: 312, &#39;apples&#39;: 430, &#39;oranges&#39;: 525}
>>> del inventory[&#39;apples&#39;]
>>> inventory
{&#39;bananas&#39;: 312, &#39;oranges&#39;: 525}
如果你想删除所有的元素,可以使用clear方法


[python]
>>> inventory.clear
<built-in method clear of dict object at 0x0000000003289588>
>>> inventory
{&#39;bananas&#39;: 312, &#39;oranges&#39;: 525}
>>> inventory.clear()
>>> inventory
{}

>>> inventory.clear
<built-in method clear of dict object at 0x0000000003289588>
>>> inventory
{&#39;bananas&#39;: 312, &#39;oranges&#39;: 525}
>>> inventory.clear()
>>> inventory
{}
使用函数len返回字典元素的数量


[python]
>>> os = {1:&#39;Linux&#39;,2:&#39;Windows&#39;}
>>> len(os)
2

>>> os = {1:&#39;Linux&#39;,2:&#39;Windows&#39;}
>>> len(os)
2

字典是可以改变的,如果你想修改字典,并且保留原来的备份,就要用到字典的copy方法,看看下面的例子:


[python]
>>> opp = {&#39;up&#39;:&#39;down&#39;,&#39;right&#39;:&#39;left&#39;,&#39;true&#39;:&#39;false&#39;}
>>> alias = opp
>>> alias
{&#39;right&#39;: &#39;left&#39;, &#39;up&#39;: &#39;down&#39;, &#39;true&#39;: &#39;false&#39;}
>>> id(opp)
53056584
>>> id(alias)
53056584
>>> other = opp.copy()
>>> other
{&#39;right&#39;: &#39;left&#39;, &#39;up&#39;: &#39;down&#39;, &#39;true&#39;: &#39;false&#39;}
>>> id(other)
52898824

>>> opp = {&#39;up&#39;:&#39;down&#39;,&#39;right&#39;:&#39;left&#39;,&#39;true&#39;:&#39;false&#39;}
>>> alias = opp
>>> alias
{&#39;right&#39;: &#39;left&#39;, &#39;up&#39;: &#39;down&#39;, &#39;true&#39;: &#39;false&#39;}
>>> id(opp)
53056584
>>> id(alias)
53056584
>>> other = opp.copy()
>>> other
{&#39;right&#39;: &#39;left&#39;, &#39;up&#39;: &#39;down&#39;, &#39;true&#39;: &#39;false&#39;}
>>> id(other)
52898824


相关文章
最新文章
热点推荐