将列表列表列入列表的字典 [英] Turning a list of lists into a dictionary of lists

查看:117
本文介绍了将列表列表列入列表的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行一个代码,遇到一个小问题。
我希望将列表列表转换为字典,其中键指向列表的原始列表中的列表位置,并且值是列表中的项目列表(从原始列表中列表)。我也希望删除原始列表中的所有Nones。例如:

  [[(1,None),(2,None)],[(0,None) 2,无)],[(1,无),(0,无)]] 

将要成为:

  {0:[1,2],1:[0,2],2: 1,0]} 


解决方案

列表理解

  raw = [[(1,无),(2,无)],[(0,无) (2,无)],[(1,无),(0,无)]] 
print {i:[el [0] for el in l] for i,l in enumerate(raw)}

打印

  {0:[1,2],1:[0,2],2:[1,0]} 


Hi there I am having a small problem with a code I am trying to implement. I wish to convert a list of lists into a dictionary where the keys refer to the lists position in the original list of lists, and the values are a list of the items that were in said list (from the original list of lists). I also wish to remove all of the Nones present in the original list of lists. For example:

[[(1, None), (2, None)], [(0, None), (2, None)], [(1, None), (0, None)]]

I would want this to become:

{0: [1, 2], 1: [0, 2], 2: [1, 0]}

解决方案

Looks like a basic dict and list comprehension

raw = [[(1, None), (2, None)], [(0, None), (2, None)], [(1, None), (0, None)]]
print {i: [el[0] for el in l] for i, l in enumerate(raw)}

prints

{0: [1, 2], 1: [0, 2], 2: [1, 0]}

这篇关于将列表列表列入列表的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆