废话不多说,直接上代码:
第一种:
数据例子:
serverList = [
{
"clusterId":1,
"redisId":2,
"ip": "10.10.1.3"
},
{
"clusterId":1,
"redisId":2,
"ip": "10.10.1.3"
},
{
"clusterId":2,
"redisId":2,
"ip": "10.10.1.3"
}
]
from collections import defaultdict
server_dict = defaultdict(list)
for server in server_list:
server_dict[server['clusterId']].append(server)
print(server_dict)
for j in server_dict:
print