list1 = [{'name': 'Tom', 'score':90}, {'name': 'Jack', 'score':86}, {'name': 'Lisa', 'score':81}, {'name': 'Bill', 'score':70}]
list2 = [{'name': 'Jack', 'score':86}, {'name': 'Bill', 'score':70}, {'name': 'Bob', 'score':48}]
怎么把 list1 和 list2 去重合并到一个新的 list_all (保持 score 的顺序),把重合的部分拆分到一个新的 list_new:
list_all = [{'name': 'Tom', 'score':90}, {'name': 'Jack', 'score':86}, {'name': 'Lisa', 'score':81}, {'name': 'Bill', 'score':70}, {'name': 'Bob', 'score':48}]
list_new = [{'name': 'Jack', 'score':86}, {'name': 'Bill', 'score':70}]
1
hareandlion 2016-12-16 12:07:38 +08:00 via iPhone
可以用 dict.update ,相同 key 的话新值会替换旧值
|
2
hareandlion 2016-12-16 12:10:56 +08:00 via iPhone
看错了不好意思,这种复杂结构只能手动遍历了吧
|
3
cxyfreedom 2016-12-16 12:15:18 +08:00 1
list_all = sorted(list1+list2, key=lambda x: x['score'], reverse=True)
list_new = [i for i in list1 if i in list2] |
4
rogwan OP @cxyfreedom 谢谢!你的 list_all 的方式不去重 ... 我在“去重”上的实现绕了好多圈,自己都看不过去了 *_*
|
5
xiaolajiao 2016-12-16 12:42:52 +08:00 2
去重这样: {v['name']:v for v in list1+list2}.values()
http://stackoverflow.com/questions/11092511/python-list-of-unique-dictionaries |
6
weyou 2016-12-16 12:45:35 +08:00 1
list_all = sorted([dict(i) for i in set([tuple(d.items()) for d in list1+list2])], key=lambda x: x['score'], reverse=True)
list_new = [i for i in list1 if i in list2] 在 @cxyfreedom 的答案上稍微修改了下,其实这么多转换,我也看不过去了 |
7
weyou 2016-12-16 12:47:04 +08:00
@xiaolajiao 这个方法好
|
8
rogwan OP @xiaolajiao 谢谢,我开始是用循环迭代去搞的
list_all = [] list1.extend(list2) for temp in list1: if temp not in list_all: list_all.append(temp) |
9
imn1 2016-12-16 12:51:04 +08:00
list_new = [i for i in list1 if i in list2]
l1 = [i for i in list1 if i not in list2] l2 = [i for i in list2 if i not in list1] list_all = list_new + l1 + l2 print(list_all) |
11
cxyfreedom 2016-12-16 12:55:58 +08:00 via iPhone
@rogwan 第一个 all 的忘记去重了,下面那个人方法不错
|
12
leeyiw 2016-12-16 12:58:40 +08:00
s1 = set(list1)
s2 = set(list2) list_all = list(s1.intersection(s2)) |
15
Hstar 2016-12-16 13:30:08 +08:00
遇到过差不多的问题, 我是把 list 里面每一项转成 json 然后用 set 去重再转回来
|
16
rogwan OP |
17
fatebe 2016-12-16 18:16:47 +08:00
for ..if .. in..
|
18
102400 2016-12-16 20:02:02 +08:00
ruby 可以直接 list1 | list2
|
19
zhx1991 2016-12-17 00:13:33 +08:00
如果要保存顺序的话好像只能循环的一个一个去看
如果不要顺序的话 linux 下 sort + comm 可以轻松做到各种集合结果 |
20
glogo 2016-12-17 03:07:05 +08:00
```
temp = [] def foo(x): if x not in temp: temp.append(x) return x print filter(foo, list1 + list2) ``` |
21
practicer 2016-12-17 10:00:03 +08:00
from operator import itemgetter
from collections import Counter # 合并列表, 思路是先将字典转为可哈希的元组 list_merged = [dict(t) for t in set([tuple(d.items()) for d in list1+list2])] # 根据字典某个值来排序, 官方库有推荐姿势 -- operator.itemgetter sorted(list_merged, key=lambda k: k['score'], reverse=True) # 没用 itemgetter sorted(list_merged, key=itemgetter('score'), reverse=True) # 使用 itemgetter # 保存重合项 list_tuple = [tuple(d.items()) for d in list1+list2] # 仍先转为元组, 使其可哈希 counts = Counter(list_tuple) # 通过 collections.Counter 查找重复项, 只接受可哈希对象 item_dups = set([i for i in list_tuple if counts[i] > 1]) # 保留出现次数大于 1 的项, 并去重 list_new = [dict(t) for t in item_dups] # 元组转回到字典对象 |
23
mark06 2016-12-19 17:18:32 +08:00
import numpy as np
list_unique=list(np.unique(np.array(list1+list2))) |
25
182247236 2016-12-21 00:30:13 +08:00
@cxyfreedom 试了下你的方法..无法去除重复,明天我也试下
|
26
182247236 2016-12-21 09:50:45 +08:00
list_new = []
list_all = [] list_only = [] list1 = [{'name': 'Tom', 'score':90}, {'name': 'Jack', 'score':86}, {'name': 'Lisa', 'score':81}, {'name': 'Bill', 'score':70}] list2 = [{'name': 'Jack', 'score':86}, {'name': 'Bill', 'score':70}, {'name': 'Bob', 'score':48}] # list_new = [i for i in list1 if i in list2] for i in list1: if i in list2: list_new.append(i) else: list_only.append(i) list_all = list_only+list_new print(list_new) print(list_all) 虽然实现了 lisr_new 和 list_all ,但是发现 lsit_all 没保持 score 的顺序,试着 list_all = sorted(list_only+list_new),结果 typeerror 。 T,T |