by wcarss · 7y ago · 0 comments
>>> from collections import defaultdict
>>> map = defaultdict(int)
>>> for word in ['hi', 'there', 'hi']:
... map[word] += 1
I am glad Java has this stuff now, but it's a little surprising that its tools are still so obtuse relative to competitors.edit: I should have foreseen the derailment caused by and nitpicking to follow a comment like this. Sorry folks!
import scala.collection.mutable.Map
> val map = Map.empty[String, Int]
> List("hi", "there", "hi").map { x =>
> map(x) = i
> }
List("hi", "there", "hi")
.zipWithIndex
.map { case (x, i) =>
x -> i
}.toMap