按组对列表排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按组对列表排序相关的知识,希望对你有一定的参考价值。
This function takes a list of string and sorts them based on their similarity. The percent at which they match can be defined in the second parameter (default 75%).
def sortByGroup(lst, percent = 75): groups = [] for item in lst: match = False for g in xrange(len(groups)): group = groups[g] parent = group[0] points = 0.0 try: for x in xrange(len(parent)): if parent[x] == item[x]: points += 1 if (points / len(parent)) * 100 >= percent: group.append(item) group.sort() match = True except: pass if not match: groups.append([item]) return groups # Example: random = [ 'bob1', 'frank2', 'bob3', 'joe2', 'frank1', 'bob2', 'joe1', 'joe3' ] groups = sortByGroup(random) for g in groups: for i in g: print i print '-' * 30 # Example Output: bob1 bob2 bob3 ------------------------------ frank1 frank2 ------------------------------ joe1 joe2 joe3 ------------------------------
以上是关于按组对列表排序的主要内容,如果未能解决你的问题,请参考以下文章