4 ms·
Could you give some examples please?
by jooke 9y ago
Could you give some examples please?
- bostik 9y agoSure, let's get a really simple one. You have N groups of something (persons, sales units, cars, tools, whatever). Find the values that are present in all of them. This problem comes up, all the time, in some form. With sets it's trivial. Take the first set, and calculate a cascading intersection with every other set. At the end you're left with only the values that appeared in every one. In very clear python the code might look like this: common = ALL_SETS[0] for _set in ALL_SETS[1:]: common = common.intersection(_set) return common Now, you can argue that underneath the hood that's still going to do N expensive iterations and you'd be right. This is where proper optimisations come in - when provided by a robust stdlib, we expect the set operations to be implemented with bitmaps.[0, 1] These are not something you'd see in a casual implementation. Btw, in the above code example I have deliberately omitted a trivial optimisation. Let's leave that as an exercise for the reader. :) 0: http://roaringbitmap.org/about/ http://roaringbitmap.org/about/ 1: https://en.wikipedia.org/wiki/Bit_array https://en.wikipedia.org/wiki/Bit_array
- keenerd 9y agoExamples like this really make me miss fold. return functools.reduce(lambda x,y: x&y, ALL_SETS)