You probably shouldn't do this in an inner loop: :-)
m = re.match(r'(\d{4})', buf)
'map' is more general than just the single, convoluted statement of list comprehensions, which really seems as a kludge from unwillingness to accept multi-line lambdas. So something new was invented that had to be learned; then it was argued it is a boon. (Like references in Perl. :-) )(There is some defaultdict functionality for autovivification in Python iirc.)
Yeah, they are all similar. I don't know enough Ruby to have an opinion.
As for regexes - while they are somewhat clumsy in python, they don't have to be defined distantly:
The only thing that really bothers me about the above is the need to separate the assignment of m from the test.Re comprehensions: seems to me they are just Python's version of Perl's map and grep.
grep:
map: Which is better? I think Ruby's way is the best, and Perl/Python are a wash here.Now, on the autovivification note, I wonder if we can make a Python class that has the desired behaviors. Like:
You don't really think it's junk, do you? Perl, Python and Ruby are extremely similar; I'd say Perl's more ergonomic, but messier; Python's cleaner, but sometimes awkward, and Ruby pretty much has the best of both worlds.