b1 = {"id": 1,"env":"PROD", "db_type":"mysql", "ip":"10.0.66.107"} b2 = {"id": 2,"env":"PROD", "db_type":"mysql", "ip":"10.0.66.117"} a = [] a.append(b1) a.append(b2) b3 = {"id": 3,"env":"TEST", "db_type":"mongodb", "ip":"10.0.66.127"} b4 = {"id": 4,"env":"TEST", "db_type":"mysql", "ip":"10.0.66.137"} a.append(b3) a.append(b4) b5 = {"id": 5,"env":"UAT", "db_type":"mysql", "ip":"10.0.66.147"} b6 = {"id": 6,"env":"UAT", "db_type":"mysql", "ip":"10.0.66.157"} a.append(b5) a.append(b6) b7 = {"id": 7,"env":"TEST", "db_type":"mysql", "ip":"10.0.66.117"} b8 = {"id": 8,"env":"TEST", "db_type":"mysql", "ip":"10.0.66.117"} a.append(b7) a.append(b8) def get_cmdblist(*cmdb_all_list, env=None, db_type=None, ip=None): # 根据 env, db_type, ip 的条件搜索返回值。如果三个参数都为空,则返回全部数据 resdict = {} if env: resdict[‘env‘] = env if db_type: resdict[‘db_type‘] = db_type if ip: resdict[‘ip‘] = ip res = [] if env == None and db_type == None and ip == None: return cmdb_all_list for item in cmdb_all_list: for i in item: #res.append(i) for k in resdict: if resdict[k] == i[k]: print(resdict[k]) #return res get_cmdblist(a, env="UAT", ip="10.0.66.147")
原文:https://www.cnblogs.com/l10n/p/15252895.html