出一道与以下代码类似的题def frequency_of_elements(lst): frequency_dict = {} for item in lst: if item in frequency_dict: frequency_dict[item] += 1 else: frequency_dict[item] = 1 return frequency_dict
时间: 2024-01-20 09:04:46 浏览: 70
process_LST_mask.zip_LST modis_MODIS裁剪_idl掩膜_modis温度_裁剪
Sure, here's an example question:
Write a function named count_vowels that takes a string and returns a dictionary with the frequency of each vowel in the string. The keys in the dictionary should be the vowels 'a', 'e', 'i', 'o', and 'u', and the values should be the frequencies of these vowels in the input string. The function should ignore case, and only count the frequency of vowels.
Here's an example implementation of the `count_vowels` function in Python:
```python
def count_vowels(s):
vowels = 'aeiou'
frequency_dict = {}
for char in s:
if char.lower() in vowels:
if char.lower() in frequency_dict:
frequency_dict[char.lower()] += 1
else:
frequency_dict[char.lower()] = 1
return frequency_dict
```
You can call this function by passing a string as an argument, like this:
```python
my_string = "The quick brown fox jumps over the lazy dog"
result_dict = count_vowels(my_string)
print(result_dict) # Output: {'e': 3, 'u': 2, 'o': 4}
```
This will calculate the frequency of vowels in the given string and return a dictionary with the keys as vowels and values as their corresponding frequencies. The function ignores the case of the characters in the input string and only counts the frequency of vowels.
阅读全文