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.特征 ...
随机推荐
- 仿qq底部的提示标记
看到一个比較不错的开源项目,分享给大家: <?xml version="1.0" encoding="utf-8"?> <RelativeLa ...
- struts2 18拦截器详解(九)
ScopedModelDrivenInterceptor 该拦截器处于defaultStack第八的位置,其主要功能是从指定的作用域内检索相应的model设置到Action中,该类中有三个相关的属性: ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- Java try-catch-finally 返回值
1.只有 try-catch 的情况下,如果不发生异常,则会返回 try 中的 return ; 如果发生异常,则会返回 catch 中的 return, try 中的 return 被盖掉; ...
- Ubuntu通过 lshw 工具包查看物理网卡名称
步骤1:安装相关工具包 apt-get install lshw lshw-gtk 步骤2:执行lshw命令进行查看硬件信息
- Creating OpenGL 4.3 window fails with GLFW3
I set up a minimal application to open a blank window with GLFW3: #include <iostream> #inclu ...
- php manager
https://phpmanager.codeplex.com/releases/view/69115
- 解决jetty runner锁定js
在web.xml中添加 <servlet> <!-- Override init parameter to avoid nasty --> <!-- file locki ...
- ORA-16038: log 3 sequence# 103 cannot be archived
[size=large]今天在自己机器做了个实验,插入10万条,由于空间少,重启数据库时出现: [size=x-large]SQL> startup ORACLE instance starte ...
- php composer工具高速使用教程,超级简单
php依赖管理工具.用于处理packages或者libraries.基于单个工程project,在project的vender目录下保存,默认永远不会全局安装. 须要php 5.3.2+,安装资源包时 ...