Python:将元组列表转换为字典列表

标签 python json d3.js bundle-layout

我正在尝试搞乱 this D3 visualization :(其代码为 here )

enter image description here

但是,我在尝试以正确的格式获取数据时遇到了困难。

数据(仅供引用)必须采用以下格式:

[
{"name":"something 1","size":number 1,"imports":["thing 1","thing 2","thing 3","thing 4","thing 5","thing 6"]},
{"name":"something 2","size":number 2,"imports":["thing 1","thing 2","thing 3","thing 4","thing 5"]}
]

现在,我的数据(截至目前)的格式为(使用 nltk 收集的):

[('would', 'MD'), ('said', 'VBD'), ('like', 'IN'), ('man', 'NN')]

使用defaultdict,我能够使用这些行转换数据:

pos = [('would', 'MD'), ('said', 'VBD'), ('like', 'IN'), ('man', 'NN')]
d = defaultdict( list )
for a, b in pos:
    d[b].append(a)
d = [ {b, d[b]} for b in d ] 

至:

[
{'MD': ['would']}, 
{'NN': ['man']}, 
{'IN': ['like']}, 
{'VBD': ['would']}
]

我不太确定如何继续或如何以正确的格式获取它。任何帮助将不胜感激。谢谢!

编辑:我应该更清楚;我的预期输出是这样的:

[
{'name': 'man', 'POS':['MD']}
]

最佳答案

类似这样的吗?

[{k, v} for k, v in list]

关于Python:将元组列表转换为字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22105664/

相关文章:

json - JQ 删除多个属性

php - 在MySQL中解析多维json字符串

javascript - D3.js 饼图周围的 flex 文本

python - 导入错误 : cannot import name 'StratifiedGroupKFold' from 'sklearn.model_selection'

python - 为 python 2.7 安装 configobj

javascript - 使用特殊字符时,服务器响应会奇怪地进行编码/解码

javascript - 将链接附加到 D3 树布局中矩形的一侧

d3.js - d3 rangeRoundBands 填充

python - 生成概率列表

javascript - 带有 Bokeh 的依赖 slider ,如何编写回调