Python 由list转为dictionary
Python 由list转为dictionary
例如:
原始的 list 形式为:
session_item_data=[[100, [10, 11], [12, 13]],
[101, [11, 12], [10, 14]],
[102, [10, 13, 14], [11, 15]]]
转为目标 dictionary的形式为:
session_item_data_dict={100: [[10, 11], [12, 13]],
101: [[11, 12], [10, 14]],
102: [[10, 13, 14], [11, 15]]}
转换的代码如下:
# !/usr/bin/env python
# encoding: utf-8
__author__ = 'Administrator' session_item_data=[[100, [10, 11], [12, 13]],
[101, [11, 12], [10, 14]],
[102, [10, 13, 14], [11, 15]]] # for curr in session_item_data:
# print(curr[0])
#
# session_index_dic=dict()
# for i in range(len(session_item_data)):
# d = session_item_data[i][0]
# # print(d)
# session_index_dic[d] = i
#
# print(session_index_dic) session_item_data_dict=dict()
for i in range(len(session_item_data)):
d = session_item_data[i][0]
print("session_id为:",d)
session_item_data_dict[d]=[[],[]]
session_item_data_dict[d][0]=session_item_data[i][1]
session_item_data_dict[d][1]=session_item_data[i][2]
print(session_item_data_dict)
输出为:

Python 由list转为dictionary的更多相关文章
- python数据类型:字典Dictionary
python数据类型:字典Dictionary 字典是一种可变容器模型,可以存储任意类型对象 键是唯一的,但是值不需要唯一 值可以取任何数据类型,但是键必须是不可变的,如字符串,数字,元组 创建字典: ...
- python 提取图片转为16 24BPP 的方法
python 中处理图片用的是 pil ,在 linux 和 win 上都可以使用. centOS 5.x 上安装的方法是 yum install python-imaging 24BPP: imp ...
- python错误之RuntimeError: dictionary changed size during iteration
pythonn报错信息: C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Ad ...
- python关于列表转为字典的两个小方法
1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...
- python学习之字典(Dictionary)练习
Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型 字典中分为键值对 , key 类型需要时被哈希. value 类型可以是 字符串.数字.元组等其他容器模 ...
- (转)使用 PyInstaller 把python程序 .py转为 .exe 可执行程序
最近使用Python为项目开发一款绘图工具(绘出 声场三维模型).因为希望能把Python脚本发布为脱离Python平台运行的可执行程序,比如单个的exe文件.PyInstaller恰满足这个需求.本 ...
- Python 访问字典(dictionary)中元素
访问python字典中元素的几种方式 一:通过“键值对”(key-value)访问: print(dict[key]) dict = {1: 1, 2: 'aa', 'D': 'ee', 'Ty': ...
- python 读取二进制文件 转为16进制输出
示例: #!/usr/bin/env python #encoding: utf-8 import binascii fh = open(r'C:\Temp\img\2012517165556.png ...
- Python将PDF转为TXT
PDFMiner----python的PDF解析器和分析器 1.官方文档:http://www.unixuser.org/~euske/python/pdfminer/index.html 2.特征 ...
随机推荐
- Maven镜像更换为阿里云中央仓库
前言 maven仓库默认在国外,使用难免很慢,尤其是下载依赖的时候,换为国内镜像,让你感受飞一般的感觉.国内支持maven镜像的有阿里云,开源中国等,这里换为阿里云的. 更换 修改maven配置文件s ...
- Android -- Toolbar跟随ListView滑动隐藏和显现
布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- spark 指定相关的参数配置 num-executor executor-memory executor-cores
num-executors参数说明:该参数用于设置Spark作业总共要用多少个Executor进程来执行.Driver在向YARN集群管理器申请资源时,YARN集群管理器会尽可能按照你的设置来在集群的 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十三)Structured Streaming遇到问题:Set(TopicName-0) are gone. Some data may have been missed
事情经过:之前该topic(M_A)已经存在,而且正常使用structured streaming消费了一段时间,后来删除了topic(M_A),重新创建了topic(M-A),程序使用新创建的top ...
- Eclipse CDT 插件列表
http://www.bestplugins.com/software/eclipse-c-plugin.html
- 腾讯下载的视频转换为MP4
第一步:首先找到腾讯视频下载设置中的缓存目录,如下图 打开这个目录,找到最近的,就是刚才你下载的文件夹 打开最近的文件夹,如下图,copy里面的内容到D盘的qlv目录中 第二部:进入D盘的qlv目录, ...
- ubuntu14.04-64位机配置android开发环境,ADT,sdk,eclipsea
这是一篇没有图的好文章,对于学习android的非常实用 1.首先到orcale官网 http://www.oracle.com/technetwork/java/javase/download ...
- springMVC 防重校验(拦截器)
<mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.bitspace. ...
- (纪录片)现代生活的秘密规则:算法 The Secret Rules of Modern Living: Algorithms
简介: The Secret Rules of Modern Living: Algorithms (2015) 导演: David Briggs主演: Marcus du Sautoy类型: 纪录片 ...
- java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)
拂晓风起 专注前端技术cocos2d.js.flash.html5,联系:kenkofox@qq.com.如果读者要找腾讯工作机会,请不吝推荐简历. 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔 ...