I've googled, I've tested, and this has me at my wits end. I have a list of numbers I need to group by similarity. For instance, in a list of [1, 6, 9, 100, 102, 105, 109, 134, 139], 1 6 9 would be put into a list, 100, 102, 105, and 109 would be put into a list, and 134 and 139. I'm terrible at math, and I've tried and tried this, but I can't get it to work. To be explicit as possible, I wish to group numbers that are within 10 values away from one another. Can anyone help? Thanks.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
First, you can easily convert any sequence into a sequence of pairs of adjacent items. Just tee it, shift it forward, and zip the unshifted and unshifted copies. The only trick is that you need to start with either
(<something>, 1)
or(139, <something>)
, because in this case we want not each pair of elements, but a pair for each element:(This isn't the simplest way to write it, but I think this may be the way that's most readable to people who aren't familiar with
itertools
.)Then, with a slightly more complicated version of Ned Batchelder's key, you can just use
groupby
.However, I think in this case this will end up being more complicated than an explicit generator that does the same thing.
There are many ways to do cluster analysis. One simple approach is to look at the gap size between successive data elements:
This will find the groups:
Note that if nums isn't actually sorted as your sample shows, you'll need to sort it first.