根据值比较字典列表

时间:2019-04-25 13:36:07

标签: python dictionary

我有两个字典列表:

old_data = [{'company': 'Amazon', 'logged_in': '2019-01-20'},
            {'company': 'Facebook', 'logged_in': '2019-04-20'},
            {'company': 'Google', 'logged_in': '2019-04-20'}]

new_data = [{'company': 'Amazon', 'logged_in': '2019-01-26'},
            {'company': 'Facebook', 'logged_in': '2019-04-12'},
            {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
            {'company': 'Wiki', 'logged_in': '2019-04-20'}]

仅在以下情况下,我才有兴趣从new_data获取列表元素:

  1. new_data中的公司不在old_data中
  2. 如果公司同时位于new_data和old_data中,则只有new_data中的登录时间晚于old_data中的时间

预期结果:

[{'company': 'Amazon', 'logged_in': '2019-01-26'},
 {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
 {'company': 'Wiki', 'logged_in': '2019-04-20'}]

到目前为止,我尝试过:

filter_data = []
for nd in new_data:
    if nd['company'] not in [d['company'] for d in old_data]:
        filter_data.append(nd)
    elif nd['company'] in [d['company'] for d in old_data]:
        date_ = # logged_in time of the company from old_data
        filter_data.append(nd if nd['logged_in']> date_)
filter_data

5 个答案:

答案 0 :(得分:2)

old_data = [{'company': 'Amazon', 'logged_in': '2019-01-20'},
            {'company': 'Facebook', 'logged_in': '2019-04-20'},
            {'company': 'Google', 'logged_in': '2019-04-20'}]

new_data = [{'company': 'Amazon', 'logged_in': '2019-01-26'},
            {'company': 'Facebook', 'logged_in': '2019-04-12'},
            {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
            {'company': 'Wiki', 'logged_in': '2019-04-20'}]

old_data = {i["company"]: i["logged_in"] for i in old_data}

result = []
for i in new_data:
    if i["company"] not in old_data:
        result.append(i)
    elif i["logged_in"] > old_data[i["company"]]:
        result.append(i)
print(result)

输出:

[{'company': 'Amazon', 'logged_in': '2019-01-26'},
 {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
 {'company': 'Wiki', 'logged_in': '2019-04-20'}]

注意:

  • old_data转换为字典以方便查找。

答案 1 :(得分:0)

如果不重复old_data中的公司名称,这是一种方法:

old_data = [{'company': 'Amazon', 'logged_in': '2019-01-20'},
            {'company': 'Facebook', 'logged_in': '2019-04-20'},
            {'company': 'Google', 'logged_in': '2019-04-20'}]

new_data = [{'company': 'Amazon', 'logged_in': '2019-01-26'},
            {'company': 'Facebook', 'logged_in': '2019-04-12'},
            {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
            {'company': 'Wiki', 'logged_in': '2019-04-20'}]
# Make dictionary mapping company names to logged in times
old_data_dict = {d['company']: d['logged_in'] for d in old_data}
# Make result by comparing logged in times to previous value or empty string
result = [d for d in new_data if d['logged_in'] > old_data_dict.get(d['company'], '')]
# Print result
print(*result, sep='\n')
# {'company': 'Amazon', 'logged_in': '2019-01-26'}
# {'company': 'LinkedIn', 'logged_in': '2019-04-20'}
# {'company': 'Wiki', 'logged_in': '2019-04-20'}

编辑:如果old_data可能包含具有相同公司名称的多个词典,则可以改为定义old_data_dict,如下所示:

old_data_dict = {}
for d in old_data:
    old_data_dict[d['company']] = max(d['logged_in'],
                                      old_data_dict.get(d['company'], ''))

答案 2 :(得分:0)

如果您使用反向索引字典old_dic,这将变得很容易:

old_data = [{'company': 'Amazon', 'logged_in': '2019-01-20'},
            {'company': 'Facebook', 'logged_in': '2019-04-20'},
            {'company': 'Google', 'logged_in': '2019-04-20'}]

new_data = [{'company': 'Amazon', 'logged_in': '2019-01-26'},
            {'company': 'Facebook', 'logged_in': '2019-04-12'},
            {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
            {'company': 'Wiki', 'logged_in': '2019-04-20'}]

old_dic = {o["company"]: {"logged_in": o["logged_in"]} for o in old_data}

result = [
    n for n in new_data 
    if n["company"] not in s or 
       n["logged_in"] > old_dic[n["company"]]["logged_in"]
]

输出:

[{'company': 'Amazon', 'logged_in': '2019-01-26'},
 {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
 {'company': 'Wiki', 'logged_in': '2019-04-20'}]

答案 3 :(得分:0)

尝试从上次停站的地方接机:

def find_logged_in(company, olddata):
    for od in olddata:
        if od['company']==company:
            return od['logged_in']
    return None


filter_data = []
for nd in new_data:
    if nd['company'] not in [d['company'] for d in old_data]:
        filter_data.append(nd)
    elif nd['company'] in [d['company'] for d in old_data]:
        date_ = find_logged_in(nd['company'], old_data) 
        if nd['logged_in'] > date_:
            filter_data.append(nd)
filter_data

结果:

[{'company': 'Amazon', 'logged_in': '2019-01-26'},
 {'company': 'LinkedIn', 'logged_in': '2019-04-20'},
 {'company': 'Wiki', 'logged_in': '2019-04-20'}]

答案 4 :(得分:0)

您可以使用熊猫来做到这一点:

from pandas import DataFrame
a = DataFrame(new_data+old_data).groupby('company',as_index=False).max().to_dict('record')
filter_data = [x for x in a if x not in b]