search

Home  >  Q&A  >  body text

python - 怎么把 list1 和 list2 中包含的重复字典去除,合并生成一个新的 list_all?

天蓬老师天蓬老师2813 days ago967

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:06:53

    # 获取list_all
    list_all = list1 + [item for item in list2 if item not in list1]
    # 显式给list_all排序
    list_all.sort(cmp=lambda x,y: cmp(x['score'], y['score']), reverse=True)
    # 获取list_new
    list_new = [item for item in list2 if item in list1]
    # 显式给list_new排序
    list_new.sort(cmp=lambda x,y: cmp(x['score'], y['score']), reverse=True)

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:06:53

    set1 = set([frozenset(i.items()) for i in list1])
    set2 = set([frozenset(i.items()) for i in list2])
    list_all = sorted(set1|set2,key = lambda k:dict(k)['score'],reverse = True)
    list_new = sorted(set1&set2,key = lambda k:dict(k)['score'],reverse = True)

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:06:53

    set() cannot handle non-hashable data and will report an error. See frozenset on the third floor

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:06:53

    list(set(list1).add(list2))

    reply
    0
  • Cancelreply