2 Star 0 Fork 0

刘元涛 / hello_python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dict4test.py 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
liuyuantao 提交于 2017-02-21 10:04 . 练习小项目初始化
#!/usr/bin/python
# -*- coding:utf-8 -*-
"""
Python字典包含了以下内置函数:
1、cmp(dict1, dict2):比较两个字典元素。
2、len(dict):计算字典元素个数,即键的总数。
3、str(dict):输出字典可打印的字符串表示。
4、type(variable):返回输入的变量类型,如果变量是字典就返回字典类型。
Python字典包含了以下内置方法:
1、dict.clear():删除字典内所有元素
2、dict.copy():返回一个字典的浅复制
3、dict.fromkeys():创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值
4、dict.get(key, default=None):返回指定键的值,如果值不在字典中返回default值
5、dict.has_key(key):如果键在字典dict里返回true,否则返回false
6、dict.items():以列表返回可遍历的(键, 值) 元组数组
7、dict.keys():以列表返回一个字典所有的键
8、dict.setdefault(key, default=None):和get()类似, 但如果键不已经存在于字典中,将会添加键并将值设为default
9、dict.update(dict2):把字典dict2的键/值对更新到dict里
10、dict.values():以列表返回字典中的所有值
"""
dic = {'tom': 11, 'sam': 57, 'lily': 100}
print(type(dic))
dic = {'lilei': 90, 'lily': 100, 'sam': 57, 'tom': 90}
for key in dic:
print(dic[key])
print(dic.items())
Python
1
https://gitee.com/liuyuantao/hello_python.git
git@gitee.com:liuyuantao/hello_python.git
liuyuantao
hello_python
hello_python
master

搜索帮助