Write a function named dl_to_ld to split a given dictionary of lists into lists of dictionaries. For example, if d = {'Macro': [88, 89, 62, 95], 'Micro': [77, 78, 84, 80]}, dl_to_ld(d) should return [{'Macro': 88, 'Micro': 77}, {'Macro': 89, 'Micro': 78}, {'Macro': 62, 'Micro': 84}, {'Macro': 95, 'Micro': 80}]
时间: 2023-08-06 07:11:28 浏览: 35
Here's a Python implementation of the dl_to_ld function:
```
def dl_to_ld(d):
# Get the keys of the dictionary
keys = list(d.keys())
# Get the length of the lists in the dictionary
n = len(d[keys[0]])
# Initialize a list of dictionaries
ld = []
# Loop over the length of the lists
for i in range(n):
# Initialize a new dictionary for each item in the list
item = {}
# Loop over the keys of the dictionary
for key in keys:
# Add the corresponding value to the new dictionary
item[key] = d[key][i]
# Append the new dictionary to the list of dictionaries
ld.append(item)
return ld
```
This function first gets the keys of the input dictionary and the length of the lists in the dictionary. It then initializes an empty list of dictionaries, and loops over the length of the lists in the dictionary. For each item in the list, it initializes a new dictionary and loops over the keys of the input dictionary, adding the corresponding value to the new dictionary. Finally, it appends the new dictionary to the list of dictionaries and returns the list.