方法一:
raw_dict={'domain_id': '$domain_id', 'enabled': '$enabled', 'name': '$username'} key_list = list(filter(lambda k: raw_dict.get(k) == "$domain_id", raw_dict.keys())) print(key_list) # ['enabled', 'name']
方法二:
newdict = dict(zip(raw_dict.values(),raw_dict.keys())) #字典的反转,但是如果两个key的value一样,反转后,只会保留一个key print(newdict)
解决问题:
接口定义的queryparm一共有3个,但是这三个参数不是非必填,测试用例需要验证分别传入这每一个参数时的返回结果,所以需要api调用时对queryparm进行移除,移除没有传入的parm
def removeParm(self,srcInterParm,parms): #去除无用的parm pattern = re.compile(r'\$(.*?)\'') parmlist =re.findall(pattern,str(srcInterParm))#获取到所有$parm变量名 noexistParm = [] for value in parmlist:#验证parm是否在传参中包含 if not parms.keys().__contains__(value): noexistParm.extend(list(filter(lambda k: srcInterParm.get(k) == '$'+value, srcInterParm.keys()))) for key in noexistParm: srcInterParm.pop(key) return srcInterParm print(t.removeParm({'domain_id': '$domain_id', 'enabled': '$enabled', 'name': '$username'},{})) #打印结果为{}