9 ms·
OK, a very common use case. As for your second part, yes, that would make my puzzle obsolete, although it's still an interesting exercise in the different ways
by jbotz 5y ago
OK, a very common use case. As for your second part, yes, that would make my puzzle obsolete, although it's still an interesting exercise in the different ways you can keep state in Python.
- remram 5y agoWhy wouldn't you use Counter for that? https://docs.python.org/3/library/collections.html#collections.Counter https://docs.python.org/3/library/collections.html#collectio...
- BugsJustFindMe 5y ago> Why wouldn't you use Counter for that? Counters are for counting, not for determining order of discovery.
- dharmab 5y ago> Changed in version 3.7: As a dict subclass, Counter Inherited the capability to remember insertion order. Math operations on Counter objects also preserve order. Results are ordered according to when an element is first encountered in the left operand and then by the order encountered in the right operand.
- BugsJustFindMe 5y ago> Changed in version 3.7: As a dict subclass, Counter Inherited the capability to remember insertion order. Yes but the question as framed ("first failed lookup initilizes to 1, the second to 2, etc") does not require counting the occurrences anymore, so using a counter does more work than necessary. If you want both counts _and_ order, then, yeah, counter is great.
- remram 5y agoI was replying to this: > The most common use case for default dict is counting the number of occurrences of some string I would assume very few people use defaultdict for this since Counter exists.
- BugsJustFindMe 5y agoOh, yeah, that's legit. Secretly (not secretly anymore, I guess!) I assume that the OP comment was a school homework assignment or something.
- fragmede 5y agoDespite attempts to depreciate Python 2, there's still a lot of code that hasn't been updated to use all of the new language features since Python 3.1, so it's anybody's guess, really. Depends on how much legacy code you interact with.