feat: - vector
This commit is contained in:
+29
@@ -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)
|
||||
Reference in New Issue
Block a user