Python---字典
来源:互联网 发布:普希金怎么死的 知乎 编辑:程序博客网 时间:2024/05/01 18:08
An object is hashable if it has a hash value which never changes during its lifetime。Amapping object mapshashable values to arbitrary objects。
字典的键是任意值,值不是hashable。值可以包含列表,字典或者其他的mutable类型。只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。字典中的键/值对是没有顺序,键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }
。注意它们 的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。
http://blog.csdn.net/facevoid/article/details/5342482链接为字典的方法总结
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}>>> keys = dishes.keys()>>> values = dishes.values()>>> # iteration>>> n = 0>>> for val in values:... n += val>>> print(n)504>>> # keys and values are iterated over in the same order>>> list(keys)['eggs', 'bacon', 'sausage', 'spam']>>> list(values)[2, 1, 1, 500]>>> # view objects are dynamic and reflect dict changes>>> del dishes['eggs']>>> del dishes['sausage']>>> list(keys)['spam', 'bacon']>>> # set operations>>> keys & {'eggs', 'bacon', 'salad'}{'bacon'}>>> keys ^ {'sausage', 'juice'}{'juice', 'sausage', 'bacon', 'spam'}test_keys = ['name','age','gender','weight','hight']test_values = ['Hongten','20','M','55','170']#使用zip来构造一个字典print('字典中的keys:')print(test_keys)print('字典中的key对应的value:')print(test_values)print('构造字典后')test_dic = dict(zip(test_keys,test_values))for key in test_dic: print( key + ':' + test_dic[key])
0 0
- Python 字典
- python 字典
- Python:字典
- Python 字典
- python字典
- Python 字典
- python 字典
- python 字典
- Python 字典
- Python字典
- python 字典
- python字典
- python 字典
- python 字典
- python--字典
- python 字典
- python字典
- Python---字典
- dp related problems (update continuously)
- 使用 C++ 处理 JSON 数据交换格式
- 第十七章 17.3.1节练习
- kettle转换之多线程
- Andrew Stankevich Contest 23 G - Rectangular Polygon
- Python---字典
- JAVA_02 线程
- uboot与系统内核mtd分区之间的关系
- 第九、十、十一周总结
- POJ 2502 Subway(最短路)
- 读《暗时间》总结
- 调试存储器转储文件
- Visual Studio扩展开发——创建自定义的tabbed工具窗
- 程序猿装 B 指南