feat: - vector

This commit is contained in:
heyong.fu
2026-05-06 11:29:40 +08:00
commit bffbdb9046
43 changed files with 17172 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
from collections import Counter, defaultdict
words = ["apple", "banana", "apple", "orange", "banana", "apple"]
# 统计每个单词出现的次数
word_counter = Counter(words)
print(word_counter) # Counter({'apple': 3, 'banana': 2, 'orange': 1})
# 获取出现次数最多的2个单词
max_word = word_counter.most_common(2)
print(max_word) # [('apple', 3), ('banana', 2)]
# 合并
merged = Counter(["a", "b", "c"]) + Counter(["a", "c"])
print(merged) # Counter({'a': 2, 'c': 2, 'b': 1})
# 创建一个以list为默认值的字典
list_dict = defaultdict(list)
list_dict["fruite"].append("apple")
list_dict["fruite"].append("banner")
print(list_dict)
count_dict = defaultdict(int)
for word in ["apple", "banana", "apple", "orange", "banana", "apple"]:
count_dict[word] += 1
print(count_dict)