我想创建一个字典来获得如下输出:
代码:
d = {}
a = [[["man", "eater", "king"], ["king", "kong", "yes"]]]
for i, x in enumerate(a):
for ii, xx in enumerate(x):
#this part i hope to check both the i and ii combination already inside the dictionary or not
#Example like if i or ii in d: # maybe something relevant
d[i] = {ii:xx}
print(d)
电流输出:
{0: {1: ['king', 'kong', 'yes']}}
预期产量:
{{0: {0: ['man', 'eater', 'king']}},{0: {1: ['king', 'kong', 'yes']}}}
我想你的意思是:
或使用嵌套的
for
循环:d
变成:请注意,您的预期输出是不正确的,因为它是一组dict,这是不可能发生的,因为dict是不可损坏的
使用
collections.defaultdict
例如:
或者
或者
输出:
相关问题 更多 >
编程相关推荐