Python学习笔记(四)生成字典
来源:互联网 发布:手机淘宝号申请注册 编辑:程序博客网 时间:2024/05/21 10:22
注:学习笔记来源于自己在学习Python过程中遇到的小问题及对网络诸多大牛提供的方法的实践。
Python 字典
一、dict函数
(1)映射
>>> items = [('a', 1), ('b', 2)]
>>> dict1 = dict(items)
>>> dict1
{'a': 1, 'b': 2}
(2)关键字参数
>>> dict1 = dict(a = 3, b = 5)
>>> dict1
{'a': 3, 'b': 5}
二、已知两个对应的list,求字典
>>> list1 = [1, 2, 2]
>>> list2 = [3, 4, 5]
>>> list3 = list (zip (list1,list2))
>>> list3
[(1, 3), (2, 4), (2,5)]
>>> dict (list3)
{1: 3, 2:5}
>>> list3 = list (zip (list2,list1))
>>> dict (list3)
{3: 1, 4: 2, 5: 2}
三、把str字符串类型变为dict字典类型
>>> a
"{'3':'1', '4':'2', '5':'1', '10':'3','14':'2', 'type':'news'}"
>>> type(a)
>>> dict1 = eval_r(a)
>>> dict1
{'10': '3', '14': '2', '3': '1', '4': '2','5': '1', 'type': 'news'}
>>> type(dict1)
注:详见 D:\Python33\fishc_learning\NLP_homework\KNNhomework.py
四、利用“收集关键字参数”来得到字典
*星号的意思是“收集其余的位置参数”
**双星号的意思是“收集关键字参数”
>>> defparams(**keypar):
>>> params(a = 1, b = 3, meter = 39)
{'a': 1, 'b': 3, 'meter': 39}
- Python学习笔记(四)生成字典
- python学习笔记(四)字典(dict)
- Python学习笔记(四)—字典(dict)
- Python学习笔记(四)——字典
- python笔记四-字典
- Python--序列学习(四)--字典
- Python学习笔记(四)——Python数据结构之字符串&字典
- Python学习笔记(四)—生成随机数
- Python学习笔记(四) 字典: 当索引不好用时
- python学习笔记 字典
- python-字典学习笔记
- python字典学习笔记
- Python 学习笔记-字典
- Python学习笔记-字典
- Python(四)字典
- python 学习笔记之(四)while循环处理列表和字典
- python 系统学习笔记(五)---字典
- python 系统学习笔记(五)---字典
- 一周小记
- 小记 半年光速的研一
- Python学习笔记(一)字典排序
- Python学习笔记(二)列表统计词频
- Python学习笔记(三)奇妙的赋值
- Python学习笔记(四)生成字典
- 9月15
- Python学习笔记之八皇后问题
- HTK学习笔记(一)在win32(win7)…
- 一些工具的下载地址
- Python学习笔记(五)正则表达式
- 拔智齿
- 基于HTK语音工具包 的学习<递进式>
- 提高libsvm预测准确率(90%引用)