Python 中文教程
写给有编程经验的开发者的 Python 教程。基于官方文档精简为 11 章,每个知识点都和你熟悉的语言对比,让你更快学会 Python。
统计出现最多的单词
from collections import Counter
text = "to be or not to be"
top = Counter(text.split()).most_common(2)
print(top) # => [('to', 2), ('be', 2)]var text = "to be or not to be";
var top = Arrays.stream(text.split(" "))
.collect(groupingBy(
w -> w, LinkedHashMap::new, counting()))
.entrySet().stream()
.sorted(comparingByValue(reverseOrder()))
.limit(2)
.toList();
System.out.println(top); // => [to=2, be=2]val text = "to be or not to be"
val top = text.split(" ")
.groupingBy { it }
.eachCount()
.entries
.sortedByDescending { it.value }
.take(2)
.map { it.key to it.value }
println(top) // => [(to, 2), (be, 2)]