d1 = {'A' : [1,2], 'B':[2,3,4],'C':[4,1,3,2], 'D':[5,6], 'E':[7,2], 'F':[8,1],'G':[1,3,4,2]}
l1 = [1,2,3,4,5,6,7,8,9]
d ={}
for i in l1:
for key,value in d1.():
for j in value:
if i in value[j]:
d.setdefault(key,[]).append(i)
预期输出:
- 1 1 <<li> c/gh>1 <<li> g/gh>2 2 b
- c 2
- e 2
但是我得到一个错误:
ValueError: not enough values to unpack (expected 2, got 1)
给出一个要点关于我想要完成的是我有一个链接列表,需要在模板中检查,并返回模板名称与链接。
这里模板名是key &数字r链接值
提前谢谢你
这是你想要做的吗?
d1 = {
'A' : [1,2],
'B':[2,3,4],
'C':[4,1,3,2],
'D':[5,6],
'E':[7,2],
'F':[8,1],
'G':[1,3,4,2]}
l1 = [1,2,3,4,5,6,7,8,9]
d ={}
for i in l1:
for key,value in d1.items():
for j in value:
if i is j:
d.setdefault(key,[]).append(i)
print(d)
输出:
{'A': [1, 2], 'C': [1, 2, 3, 4], 'F': [1, 8], 'G': [1, 2, 3, 4], 'B': [2, 3, 4], 'E': [2, 7], 'D': [5, 6]}
我编辑了我的原始答案来匹配你的澄清:
d1 = {'A' : [1,2], 'B':[2,3,4],'C':[4,1,3,2], 'D':[5,6], 'E':[7,2], 'F':[8,1],'G':[1,3,4,2]}
l1 = [1,2,3,4,5,6,7,8,9]
d = []
for i in l1:
for k, v in d1.items():
if i in v:
d.append((k,i))
print(d)
输出
[('A', 1), ('C', 1), ('F', 1), ('G', 1), ('A', 2), ('B', 2), ('C', 2), ('E', 2), ('G', 2), ('B', 3), ('C', 3), ('G', 3), ('B', 4), ('C', 4), ('G', 4), ('D', 5), ('D', 6), ('E', 7), ('F', 8)]