I'm just studying Python and found some place even not as convenient as Java8, e.g. word count
At first I thought it may be very easy to implement just like
>>> {x : x**2 for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
But actually I found it is a little cumbersome
>>> sent3
['In', 'the', 'beginning', 'God', 'created', 'the', 'heaven', 'and', 'the', 'earth', '.']
>>> for w in sent3:
... if w in word_count:
... word_count[w] += 1
... else:
... word_count[w] = 1
...
But in Java8 it's very convenience to implement it,
List<String> strings = asList("In", "the", "beginning", "God", "created", "the", "heaven", "and", "the", "earth");
Map<String, Long> word2CountMap = strings.stream().collect(groupingBy(s -> s, counting()));
or
word2CountMap = new HashMap<>();
for (String word : strings) {
word2CountMap.compute(word, (k, v) -> v == null ? 1 : v + 1);
}
I want to know if exist some advanced usage of Python dict
could implement it more easily that I do not know?