list=(("memory","1","16g"), ("memory","2","32g"), ("color","15","red"), ("color","16","blue")) res={} for info in list: if(res.has_key(info[0])): res[info[0]]["ids"].append(info[1]) res[info[0]]["values"].append(info[2]) else: item={"ids":[info[1]],"values":[info[2]]} res[info[0]]=item print res
结果:
{'color': {'values': ['red', 'blue'], 'ids': ['15', '16']}, 'memory': {'values': ['16g', '32g'], 'ids': ['1', '2']}}