【发布时间】:2022-01-11 16:14:50
【问题描述】:
输入:
values = [{'priority': 'P2', 'subdomain': 'SD2', 'test': '2653'},
{'priority': 'P3', 'subdomain': 'SD1', 'test': '2656'},
{'priority': 'P2', 'subdomain': 'SD2', 'test': '2651'},
{'priority': 'P2', 'subdomain': 'SD2', 'test': '2652'},
{'priority': 'P3', 'subdomain': 'SD1', 'test': '2655'}]
应转换为:
values = [{'priority': 'P2', 'subdomain': 'SD2', 'test': '2653,2651,2652'},
{'priority': 'P3', 'subdomain': 'SD1', 'test': '2656,2655'}]
key 的值不固定,会根据用户的需要而变化。
我试过了:
result_dict["test"] = values[0]["test"]
final_output = []
I = 1
for value in range(len(values)):
print("value", value)
for j in range(I, len(values)):
print("j", j)
I = I+1
if values[value]["priority"] == values[j]["priority"] \
and values[value]["subdomain"] == values[j]["subdomain"]:
final_output = []
result_dict["priority"] = values[value]["priority"]
result_dict["subdomain"] = values[value]["subdomain"]
result_dict["test"] = result_dict["test"]+","+values[j]["test"]
count = count +1
final_output.append(result_dict)
print(final_output)
请帮帮我。
【问题讨论】:
-
您真的希望
'test'是一个字符串,例如'2653,2651,2652',而不是一个列表[2653,2651,2652]或['2653','2651','2652']?
标签: python arrays list sorting dictionary