Python 标准库

很好的python标准库资源网站
https://pymotw.com/3/

Python标准库 是一组模块, 安装的Python都包含它。可使用标准库中的任何函数和类, 为此只需在程序开头包含一条简单import 语句。 下面来看模块collections 中的一个类OrderedDict。
创建字典并记录其中的键—值对的添加顺序

代码

# -*- coding:utf-8 -*-

from collections import OrderedDict # 从模块collections导入OrderedDict类

favorite_languages = OrderedDict() # 创建OrderedDict类的实例。创建空的有序字典

favorite_languages['jen'] = 'python' # 添加名字-语言对
favorite_languages['sarah'] = 'c'
favorite_languages['edward'] = 'ruby'
favorite_languages['phil'] = 'java'

for name, language in favorite_languages.items(): # 遍历favorite_languages
    print name.title() + "'s favorite language is " + \
        language.title() + "."

结果

Jen's favorite language is Python.
Sarah's favorite language is C.
Edward's favorite language is Ruby.
Phil's favorite language is Java.

你可能感兴趣的:(Python 标准库)