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的更多相关文章

  1. python数据类型:字典Dictionary

    python数据类型:字典Dictionary 字典是一种可变容器模型,可以存储任意类型对象 键是唯一的,但是值不需要唯一 值可以取任何数据类型,但是键必须是不可变的,如字符串,数字,元组 创建字典: ...

  2. python 提取图片转为16 24BPP 的方法

    python 中处理图片用的是 pil ,在 linux  和 win 上都可以使用. centOS 5.x 上安装的方法是 yum install python-imaging 24BPP: imp ...

  3. python错误之RuntimeError: dictionary changed size during iteration

    pythonn报错信息: C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Ad ...

  4. python关于列表转为字典的两个小方法

    1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...

  5. python学习之字典(Dictionary)练习

    Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型 字典中分为键值对 , key 类型需要时被哈希. value 类型可以是 字符串.数字.元组等其他容器模 ...

  6. (转)使用 PyInstaller 把python程序 .py转为 .exe 可执行程序

    最近使用Python为项目开发一款绘图工具(绘出 声场三维模型).因为希望能把Python脚本发布为脱离Python平台运行的可执行程序,比如单个的exe文件.PyInstaller恰满足这个需求.本 ...

  7. Python 访问字典(dictionary)中元素

    访问python字典中元素的几种方式 一:通过“键值对”(key-value)访问: print(dict[key]) dict = {1: 1, 2: 'aa', 'D': 'ee', 'Ty': ...

  8. python 读取二进制文件 转为16进制输出

    示例: #!/usr/bin/env python #encoding: utf-8 import binascii fh = open(r'C:\Temp\img\2012517165556.png ...

  9. Python将PDF转为TXT

    PDFMiner----python的PDF解析器和分析器 1.官方文档:http://www.unixuser.org/~euske/python/pdfminer/index.html 2.特征 ...

随机推荐

  1. Double-Array Trie 原理解析

     http://ansjsun.iteye.com/blog/702255     Trie树是搜索树的一种,它在本质上是一个确定的有限状态自动机,每个结点代表一个状态,根据输入变量的不同,进行状态转 ...

  2. android studio开发的时候出现design editor is unavailable until after a successful project sync问题的解决方法

    android studio设计界面的时候,出现了报错:design editor is unavailable until after a successful project sync,导致无法编 ...

  3. iOS开发技巧 - 使用UIDatePicker来选择日期和时间

    (Swift) import UIKit class ViewController: UIViewController { var datePicker: UIDatePicker! func dat ...

  4. hadoop old API CombineFileInputFormat

    来自:http://f.dataguru.cn/thread-271645-1-1.html 简介 本文主要介绍下面4个方面 1.为什么要使用CombineFileInputFormat 2.Comb ...

  5. 如何根据搜索页面内容得到的结果生成该元素的xpath路径

    如何根据搜索页面内容得到的结果生成该元素的xpath路径?

  6. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列三:重置主从同步

    1:停止slave服务器的主从同步 stop slave; 2:对Master数据库加锁 flush tables with read lock; 3:备份Master上的数据 mysqldump - ...

  7. 一个简单的 JSON 生成/解析库

    这是一个单文件的,适用于C语言的, JSON 读写库. 先说明,不想造轮子,代码是从这里拿来的: https://www.codeproject.com/Articles/887604/jWrite- ...

  8. The Builder pattern simulates named optional parameters(Java)

    the Builder pattern is a good choice when designing classes whose constructors or static factories w ...

  9. jackson 中JsonFormat date类型字段的使用

    为了便于date类型字段的序列化和反序列化,需要在数据结构的date类型的字段上用JsonFormat注解进行注解具体格式如下 @JsonFormat(pattern = "yyyy-MM- ...

  10. 〖Android〗Android源代码所有目录生成的Target(编译生成文件反查)

    => build/tools/zipalign: out/host/linux-x86/bin/zipalign (host) => build/tools/atree: out/host ...