在django中集成多个词典
这些天来,我在列表方面遇到了很多麻烦,我是一名php开发人员,正在python中寻找一席之地。我的问题与我的工作有关 我现在有了一个字典组,它由id\u位置和包含顺序[Top,Right,Bottom,Left,Center]的标志组成:在django中集成多个词典,django,list,dictionary,Django,List,Dictionary,这些天来,我在列表方面遇到了很多麻烦,我是一名php开发人员,正在python中寻找一席之地。我的问题与我的工作有关 我现在有了一个字典组,它由id\u位置和包含顺序[Top,Right,Bottom,Left,Center]的标志组成: a = {'41': [0, 0, 0, 0, 1], '42': [0, 0, 1, 0, 1], '43': [0, 0, 0, 0, 1], '44': [0, 0, 0, 0, 1]} 以及其他包含我的id\u位置和状态的字典: b = {'4
a = {'41': [0, 0, 0, 0, 1], '42': [0, 0, 1, 0, 1], '43': [0, 0, 0, 0, 1], '44': [0, 0, 0, 0, 1]}
以及其他包含我的id\u位置和状态的字典:
b = {'44': 'statusC', '42': 'statusB', '41': 'statusA', '43': 'statusC'}
我想在我的代码中包含dict A,以便在下面保存dict B
for pos, stat in B.items():
MyModel.objects.create(position=pos, status=stat, Top = "" , Right="" Bottom = "", Left= "")
我怎么做这个锅?
你能推荐我从php到django的学习清单吗
更新:
我遵循并在下面添加了我的代码:
c = {}
for key in set().union(a, b):
if key in a: c.setdefault(key, []).extend(a[key])
if key in b: c.setdefault(key, []).extend(b[key])
print(c)
它返回:
{
'42': [0, 0, 1, 0, 1, 's', 't', 'a', 't', 'u', 's', 'B'],
'41': [0, 0, 0, 0, 1, 's', 't', 'a', 't', 'u', 's', 'A'],
'44': [0, 0, 0, 0, 1, 's', 't', 'a', 't', 'u', 's', 'C'],
'43': [0, 0, 0, 0, 1, 's', 't', 'a', 't', 'u', 's', 'C']
}
我现在的问题是我的字符串被分隔了不确定您要查找什么 像这样的
Top=A[int(pos)][0]
Right=A[int(pos)][1]
Bottom=A[int(pos)][2]
Left=A[int(pos)][3]
希望有帮助。它返回了
keyrerror:id
您的一个dict不符合给定的描述。我还注意到,在B dict中,您的键是字符串,而在A dict中,它们是整数。相应地更新了答案