defcount_elements(nums):
count ={}for num in nums:
count[num]= count.get(num,0)+1return count
numbers =[1,2,3,2,1,3,4,5,2,1]
result = count_elements(numbers)print(result)# 输出:{1: 3, 2: 3, 3: 2, 4: 1, 5: 1}
单词频率统计:统计文本中每个单词的出现次数。
defword_frequency(text):
words = text.split()
frequency ={}for word in words:
word = word.lower()
frequency[word]= frequency.get(word,0)+1return frequency
text ="This is a test sentence. This is another sentence."