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.特征 ...
随机推荐
- 网页重构应该避免的10大 CSS 糟糕用法
对于网页重构来说,CSS禅意花园 是网页布局从 table 表格转到了 html +css 的标志 .这些年来,随着我们的网站越来越复杂:html5,css3,新的技术.新的属性,越来越多的开发者开始 ...
- springboot下配置多数据源
摘自: http://blog.csdn.net/wangqingqi20005/article/details/52613055
- C# Winform 未能加载文件或程序集"System.Data.SQLite"或它的某一个依赖项。试图加载格式不正确的程序
在使用Winform 开发了一个小软件,其中使用了SQLite作为数据库 但在我的Win7 64位系统上却出现了以下错误: System.BadImageFormatException: 未能加载文件 ...
- CHtmlEditCtrl (3): More HTML Editor Options
In this version of our HTML Editor, we'll create a floating source view/edit window and we'll implem ...
- 前端html用一个表单来映射后台多个对象
public class entity1 { private String id; public String getId() { return id; } public void setId(Str ...
- RobotFramework+Selenium2+Appium环境搭建
转载:https://www.cnblogs.com/testway/p/7372326.html 装python 2.7 RobotFramework是python2 写的,图形界面使用的wxpyt ...
- 比特币 Bitcoin 是什么,我勒个去,哈耶克果然超前——货币的非国有化,容我思量一下【转载+整理】
原文地址 比特币矿业史(上):故事的开始,CPU 时代 比特币矿业史(中):群众的觉醒 ,GPU 时代 比特币矿业史(下):巨头的诞生 ,ASIC 时代 本文内容 引子 0 序 1 故事的开始 : C ...
- STL 笔记(四) 迭代器 iterator
stl 中迭代器能够理解为面向对象版本号的广义指针,提供了对容器中的对象的訪问方法,能够遍历容器全部元素.也能够訪问随意元素.stl 迭代器有下面五种: Input iterators 仅仅读,输 ...
- 【推荐】ImageProcessor.Web,再也不用自己生成缩略图了
1.什么是ImageProcessor.Web ImageProcessor.Web是基于ImageProcessor的web图像处理模块,允许开发者使用URL查询字符串参数的方式作为指令执行图像处理 ...
- ThreadLocal MDC
因为MDC底层是用ThreadLocal实现的,所以这里补充一些和ThreadLocal相关的知识点. 1.ThreadLocal的三个层次 关于ThreadLocal有三个层次,可以按照这三个层次去 ...