出一道类似的题Write a function named frequency_of_elements that takes a list and returns the frequency of elements of this list in a dictionary, where the keys are elements, and values are the frequencies of corresponding elements.
时间: 2024-02-03 21:02:07 浏览: 25
Sure! Here's a Python function that does what you're asking for:
```python
def frequency_of_elements(lst):
freq_dict = {}
for elem in lst:
if elem in freq_dict:
freq_dict[elem] += 1
else:
freq_dict[elem] = 1
return freq_dict
```
This function takes a list `lst` as input, and returns a dictionary `freq_dict` where the keys are the elements of the list, and the values are the frequencies of those elements in the list. For example:
```python
>>> frequency_of_elements([1, 2, 3, 2, 1, 3, 3, 4])
{1: 2, 2: 2, 3: 3, 4: 1}
```
In this example, the input list contains four different elements: 1, 2, 3, and 4. The output dictionary shows that the element 1 appears twice in the list, the element 2 appears twice, the element 3 appears three times, and the element 4 appears once.