我不知道如何编写用于比较列表中存在的两个字典的脚本,即使我不知道字典名称
示例代码: 我是否正确?如果没有那么请帮我找到解决方案 这里“dct_list_cluster”是包含两个词典的列表
码
for count in range(len(dct_list_cluster)):
if dct_list_cluster[count].keys() in dct_list_cluster[count+1].keys():
fo = open("cluster_" + str(ip_list[count]) + "_output.txt", "a")
fo.write("\n=> %s" % (dct_list_cluster[key])
答案 0 :(得分:0)
如果我说对了你
您可以使用list comprehension
<强>码强>
lst=[{"a":2,"b":3,"c":4},{"b":4}]
[a for a in lst[0] if a in lst[1]]
['b']
完成list comprehension
<强>码强>
lst=[{"a":2,"b":3,"c":4},{"b":4}]
for a in lst[0]:
if a in lst[1]]:
print a
<强>输出:强>
b
<强>操作:强>
1.当你在字典上循环时,你正在循环遍历字典的键there are methods to loop over value and both keys and value
2.如果是第二个字典,请查看是否可以打印
修改强>
lst=[{"a":2,"b":3,"c":4},{"b":4},{"b":2,"d":6},{"d":4}]
for count in range(len(lst)-1):
for a in lst[count]:
if a in lst[count+1]:
print "dic"+str(count)+"\t"+str(a)+"\tis common to next dic"
<强>输出:强>
dic0 b is common to next dic
dic1 b is common to next dic
dic2 d is common to next dic