How to merge similar items in a list

后端 未结 6 591
花落未央
花落未央 2021-01-19 02:50

I haven\'t found anything relevant on Google, so I\'m hoping to find some help here :)

I\'ve got a Python list as follows:

[[\'hoose\', 200], [\"Ba         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 03:10

    You didn't say the number of items in your list, but I'm guessing n^2 complexity is OK.

    You also didn't say if you wanted all possible pairs to be compared or just the neighboring ones. I assume all pairs.

    So here's the idea:

    1. Take the first item, and calculate the lev score against all other items.
    2. Merge all items which score is less than 5, by removing them from the list and summing their scores.
    3. In the merged list, take the next item, compare that one to all items except the one you just checked.
    4. Repeat until there are no items in the list

提交回复
热议问题