Patch Share: Most common item in a list
Alec Gordon
Nov 19 2023 | 3:18 am
This isn't a question, just posting an algorithm I lost some time to making. Hopefully I can save someone else some time. This is a way to find the most common item in a list, and I'm sure it can be expanded to reform a list by most to least common. If anyone has a more elegant way of doing this, it would be cool to see it.
- TFLNov 19 2023 | 8:03 amNot on my computer right now, but thinking about another, more powerful way, which work with any kind of item (no need to provide a list of what list items can be):- [zl.iter] 1 through your list - for each, check if it's in a [coll]. If not, create a new entry with that item in the coll. If yes, add 1 to the corresponding entry. You might need to use [tosymbol] just after the zl.iter in order to treat numbers and symbol items the same way. At the end you have a [coll] filled more or less like this: item1 4 item2 1 item3 8 item4 2 - Sort the list by its second column (Which is the number of apparitions of that item) with the message "sort -1 0" - do whatever you want with that coll (request line N to get the Nth most common item, reconstruct the list with items sorted by number of apparition...)Will give it a try when I'm back on my computer!
- TFLNov 19 2023 | 8:06 amAlso, plenty of other solutions by searching "list most common" in the forum (Patcher examples, externals...).
- Source AudioNov 20 2023 | 10:08 amcoll is also my prefered tool, if unix text tools are not to be usedMax Patcher
In Max, select New From Clipboard. - Source AudioNov 20 2023 | 1:40 pmI use delays because they guarantee execution of further messages if objects can't report "done". using trigger alone is not safe enough for me, as it proved over the years. I even don't bother to check if timing is ok without them.
- Jean-Francois CharlesNov 20 2023 | 2:12 pmVariation on the theme, here using the 'max' message to [coll] instead of sorting everything.Max Patcher
In Max, select New From Clipboard. - Source AudioNov 20 2023 | 2:22 pmmax message reports only 1 list member, if multiple have same max number of occurances in the list that fails....
- Jean-Francois CharlesNov 20 2023 | 3:12 pmIndeed - merge on the second coll is elegant! (plus there is a bug in the version I posted)
- Source AudioNov 20 2023 | 3:25 pmLobjects had Lmost which also reported only single item in case multiple items were at top, one with lower 1st index in the list would be prefered pick.
- Roman ThileniusNov 20 2023 | 11:11 pmjust to make sure i understand the task."most common" means that 1. it must be present in both lists and 2. among those common ones find the one which has the overall highest occuranceright?and what should happen if there are multiple winners?
- Jean-Francois CharlesNov 21 2023 | 4:04 amAlec was just looking for the most common element in a single given list. 2 lists in the patch to serve as test. The patch given by Source Audio is cool in that it gives the list of all most common elements in case there are several different elements appearing the same amount of times (and the most).