dongzhong8834 2013-10-01 14:33
浏览 686

根据相似度最高的值对字典列表进行排序

Given the following python list of dictionaries:

results = [[{'id': '001', 'result': [0,0,0,0,1]},
           {'id': '002', 'result': [1,1,1,1,1]},
           {'id': '003', 'result': [0,1,1,None,None]},
           {'id': '004', 'result': [0,None,None,1,0]},
           {'id': '005', 'result': [1,0,None,1,1]},
           {'id': '006', 'result': [0,0,0,1,1]}],
          [{'id': '001', 'result': [1,0,1,0,1]},
           {'id': '002', 'result': [1,1,1,1,1]},
           {'id': '003', 'result': [0,1,1,None,None]},
           {'id': '004', 'result': [0,None,None,1,0]},
           {'id': '005', 'result': [1,0,None,1,1]},
           {'id': '006', 'result': [1,0,1,0,1]}]
            ]

I would like to generate a new sorted list (in both python and golang) based on the values of 'result' by comparing results between the players ('id') in each group and then sorting them based on the number of matching entries (None results are discarded and not counted):

During the first round and second round 001 and 006 had nine matching answers:
001 = [0,0,0,0,1] 006 = [0,0,0,1,1] - four matching answers.
During the second round, 001 and 006 had five matching answers:
001 = [1,0,1,0,1] 006 = [1,0,1,0,1] - five matching answers

sorted_results = ['001','006','002','005','003','004']

'001' and '006' are the first two items in the list because they have the highest number of matching results - nine.

  • 写回答

2条回答 默认 最新

  • duanjiaopi8218 2013-10-01 14:40
    关注

    If you sort those items by the "highest number of identical results", this is what you get:

    ['003', '004', '005', '006', '001', '002']
    

    If you meant something else (i.e. not "highest number of identical results"), please clarify your question. Also, you can simply modify the max_identical function so that it acts according to your definition of similar.

    The above result was computed with:

    from collections import defaultdict
    
    
    results = [{'id': '001', 'result': [0, 0, 0, 0, 1]},
               {'id': '002', 'result': [1, 1, 1, 1, 1]},
               {'id': '003', 'result': [0, 1, 1, None, None]},
               {'id': '004', 'result': [0, None, None, 1, 0]},
               {'id': '005', 'result': [1, 0, None, 1, 1]},
               {'id': '006', 'result': [0, 0, 0, 1, 1]}]
    
    
    def max_identical(lst):
        counts = defaultdict(lambda: 0)
        for x in lst:
            if x is not None:
                counts[x] += 1
        return max(counts.values())
    
    
    results = sorted(results, key=lambda x: max_identical(x['result']))
    
    print [x['id'] for x in results]
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法