首页 > 代码库 > Python 字典

Python 字典

Python 字典

1、字典可以看作是无序的列表集合,字典是通过键来存取,而不是通过偏移来存取。

2、任意对象的无序集合,可变长、异构、任意嵌套。

3、属于可变映射类型(key-value),但不支持序列操作(合并,分片)。

4、字典基本操作

In [1]: D={001:egg,002:apple,003:orange,004:banana,000:minion}

In [2]: D[002]
Out[2]: apple

In [3]: D
Out[3]:
{000: minion,
 001: egg,
 002: apple,
 003: orange,
 004: banana}

In [4]: len(D)
Out[4]: 5

In [5]: orange in D
Out[5]: False

In [6]: apple in D
Out[6]: False

In [7]: 001 in D
Out[7]: True

In [9]: list(D.keys())
Out[9]: [001, 002, 003, 004, 000]

In [10]: D[003]=[good orange,bad orange]

In [11]: D
Out[11]:
{000: minion,
 001: egg,
 002: apple,
 003: [good orange, bad orange],
 004: banana}

In [12]: del D[000]

In [13]: D
Out[13]:
{001: egg,
 002: apple,
 003: [good orange, bad orange],
 004: banana}

In [14]: D[000]=minion

In [15]: D
Out[15]:
{000: minion,
 001: egg,
 002: apple,
 003: [good orange, bad orange],
 004: banana}

 

Python 字典