Let us Suppose, I have created 3 lists and I want to create a dictionary for it. e.g.
让我们假设,我创建了3个列表,我想为它创建一个字典。例如
a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
Now What I want is to create a dictionary like this:
现在我想要的是创建一个这样的字典:
{'A':{'1' :'9'} , 'B':{'2':'8'}, 'C':{'3':'7'} , 'D':{'4':'6'}}
is it possible, Can Someone Help me on this?
是否有可能,有人可以帮助我吗?
7 个解决方案
#1
3
You can create the dictionary from zip
-ed lists and convert the int values to strings - if I understood your question proper
你可以从zip-ed列表创建字典,并将int值转换为字符串 - 如果我理解你的问题
dct = {x: {str(y): str(z)} for x, y, z in zip(a,b,c)}
Output:
输出:
{'A': {'1': '9'}, 'C': {'3': '7'}, 'B': {'2': '8'}, 'D': {'4': '6'}}
#2
2
a = ['A', 'B', 'C', 'D'] # don't forget the quotation marks
b = [1, 2, 3, 4]
c = [9, 8, 7, 6]
res = dict()
for i, index_a in enumerate(a):
res[index_a] = {str(b[i]): c[i]}
Edit: Alternatively with list comprehension (mainly for the voters in here, as it's advanced python and harder to understand):
编辑:或者使用列表理解(主要针对这里的选民,因为它是高级python并且更难理解):
res = dict((a[i], {str(b[i]): c[i]}) for i in range(len(a)))
#3
2
You can also use map()
here:
你也可以在这里使用map():
a = ['A', 'B', 'C', 'D']
b = [1, 2, 3, 4]
c = [9, 8, 7, 6]
dct = dict(map(lambda x, y, z : (x, {str(y): str(z)}), a, b, c))
print(dct)
Which outputs:
哪个输出:
{'A': {'1': '9'}, 'B': {'2': '8'}, 'C': {'3': '7'}, 'D': {'4': '6'}}
#4
2
Assuming what you want is to have a
be keys in the outer dictionary, and b
and c
the key and value element of the inner dicts:
假设你想要的是在外部字典中有一个be键,而b和c是内部dicts的键和值元素:
d = {k: {x: y} for k, x, y in zip(a, b, c)}
Update: However, in your example x
and y
are strings, so if that's what you want:
更新:但是,在您的示例中,x和y是字符串,所以如果这是您想要的:
d = {k: {str(x): str(y)} for k, x, y in zip(a, b, c)}
#5
1
{ a[x]: {b[x]: c[x]} for x in range(len(a))}
or if you really mean it:
或者如果你真的是这个意思:
{ a[x]: {str(b[x]): str(c[x])} for x in range(len(a))}
#6
1
Are you looking for something like this ?
你在找这样的东西吗?
a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
new_dict={}
set(map(lambda x,y,z:(new_dict.__setitem__(x,{y,z})),a,b,c))
print(new_dict)
output:
输出:
{'D': {4, 6}, 'A': {9, 1}, 'B': {8, 2}, 'C': {3, 7}}
#7
1
You can try this:
你可以试试这个:
a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
new_data = dict([[a, dict([map(str, i)])] for a, i in zip(a, zip(b, c))])
Output:
输出:
{'A': {'1': '9'}, 'C': {'3': '7'}, 'B': {'2': '8'}, 'D': {'4': '6'}}
Or
要么
new_data = dict(zip(a, map(lambda x:dict([x]), zip(map(str, b), map(str, c)))))
#1
3
You can create the dictionary from zip
-ed lists and convert the int values to strings - if I understood your question proper
你可以从zip-ed列表创建字典,并将int值转换为字符串 - 如果我理解你的问题
dct = {x: {str(y): str(z)} for x, y, z in zip(a,b,c)}
Output:
输出:
{'A': {'1': '9'}, 'C': {'3': '7'}, 'B': {'2': '8'}, 'D': {'4': '6'}}
#2
2
a = ['A', 'B', 'C', 'D'] # don't forget the quotation marks
b = [1, 2, 3, 4]
c = [9, 8, 7, 6]
res = dict()
for i, index_a in enumerate(a):
res[index_a] = {str(b[i]): c[i]}
Edit: Alternatively with list comprehension (mainly for the voters in here, as it's advanced python and harder to understand):
编辑:或者使用列表理解(主要针对这里的选民,因为它是高级python并且更难理解):
res = dict((a[i], {str(b[i]): c[i]}) for i in range(len(a)))
#3
2
You can also use map()
here:
你也可以在这里使用map():
a = ['A', 'B', 'C', 'D']
b = [1, 2, 3, 4]
c = [9, 8, 7, 6]
dct = dict(map(lambda x, y, z : (x, {str(y): str(z)}), a, b, c))
print(dct)
Which outputs:
哪个输出:
{'A': {'1': '9'}, 'B': {'2': '8'}, 'C': {'3': '7'}, 'D': {'4': '6'}}
#4
2
Assuming what you want is to have a
be keys in the outer dictionary, and b
and c
the key and value element of the inner dicts:
假设你想要的是在外部字典中有一个be键,而b和c是内部dicts的键和值元素:
d = {k: {x: y} for k, x, y in zip(a, b, c)}
Update: However, in your example x
and y
are strings, so if that's what you want:
更新:但是,在您的示例中,x和y是字符串,所以如果这是您想要的:
d = {k: {str(x): str(y)} for k, x, y in zip(a, b, c)}
#5
1
{ a[x]: {b[x]: c[x]} for x in range(len(a))}
or if you really mean it:
或者如果你真的是这个意思:
{ a[x]: {str(b[x]): str(c[x])} for x in range(len(a))}
#6
1
Are you looking for something like this ?
你在找这样的东西吗?
a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
new_dict={}
set(map(lambda x,y,z:(new_dict.__setitem__(x,{y,z})),a,b,c))
print(new_dict)
output:
输出:
{'D': {4, 6}, 'A': {9, 1}, 'B': {8, 2}, 'C': {3, 7}}
#7
1
You can try this:
你可以试试这个:
a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
new_data = dict([[a, dict([map(str, i)])] for a, i in zip(a, zip(b, c))])
Output:
输出:
{'A': {'1': '9'}, 'C': {'3': '7'}, 'B': {'2': '8'}, 'D': {'4': '6'}}
Or
要么
new_data = dict(zip(a, map(lambda x:dict([x]), zip(map(str, b), map(str, c)))))