简体   繁体   English

如何从特定键值的两个字典列表中获取特定对象?

[英]How to get specific objects from two list of dictionaries on a specific key value?

I have two lists of dictionaries:我有两个字典列表:

timing = [
    {"day_name": "sunday"},
    {"day_name": "monday"},
    {"day_name": "tuesday"},
    {"day_name": "wednesday"},
    {"day_name": "thursday"},
    {"day_name": "friday"},
    {"day_name": "saturday"},
]

hours_detail = [
    {"day_name": "sunday", "peak_hour": False},
    {"day_name": "monday", "peak_hour": False},
    {"day_name": "tuesday", "peak_hour": False},
    {"day_name": "wednesday", "peak_hour": False},
    {"day_name": "thursday", "peak_hour": False},
    {"day_name": "friday", "peak_hour": False},
    {"day_name": "saturday", "peak_hour": False},

    {"day_name": "saturday", "peak_hour": True},
    {"day_name": "friday", "peak_hour": True},
    {"day_name": "thursday", "peak_hour": True},
]

I want to create another list of dictionaries that looks like the one below.我想创建另一个字典列表,如下所示。 I'm basically combining these two lists and also rearranging according to the day name.我基本上是将这两个列表结合起来,并根据日期名称重新排列。

final_data_object = [
    {
        "timing": {"day_name": "saturday"},
        "hour_detail": [
            {"day_name": "saturday", "peak_hour": False},
            {"day_name": "saturday", "peak_hour": True},
        ]
    },
    {
        "timing": {"day_name": "friday"},
        "hour_detail": [
            {"day_name": "friday", "peak_hour": False},
            {"day_name": "friday", "peak_hour": True},
        ]
    },
    soon on...
]

I have tried this but it didn't work:我试过这个但没有用:

data = []
for time_instance in timing:
    obj = {
        "timing": time_instance
    }
    for hour_instance in hour_detail:
        if time_instance["day_name"] == hour_instance["day_name"]:
            obj["hour_detail"] = hour_instance
            data.append(obj)

return data

If pricing = hour_detail then obj["pricing"] must be a list as it can have multiple value.如果pricing = hour_detail那么obj["pricing"]必须是一个列表,因为它可以有多个值。

You have to create a new list in your loop for time_instance in timing: and append every time time_instance["day_name"] == pricing_instance["day_name"] .您必须在循环for time_instance in timing:和 append 每次time_instance["day_name"] == pricing_instance["day_name"]创建一个新列表。

Exemple:例子:

data = []

for time_instance  in timing:
    current_hour_detail = []
    for line in hours_detail:
        if line["day_name"] == time_instance["day_name"]:
            current_hour_detail.append(line)
            
    data.append({
        "timing": time_instance,
        "pricing": current_hour_detail
    })

Or smaller way或者更小的方式

data = []

for time_instance  in timing:
    current_hour_detail = [line for line in hours_detail if line["day_name"] == time_instance["day_name"]]
    data.append({
        "timing": time_instance,
        "pricing": current_hour_detail
    })

Whatch out tho, this solution is in O(n*m) time complexity, by first transforming your list hours_detail by a hashtable {day_name: [peak_hour1, peak_hour2]} you can reduce it in O(n+m)不过,此解决方案的时间复杂度为 O(n*m),首先通过哈希表 {day_name: [peak_hour1, peak_hour2]} 转换列表hours_detail ,您可以将其减少为 O(n+m)

This should answer you question:这应该回答你的问题:

...
data = []
for time_instance in timing:
    obj = {"timing": time_instance}
    # use a list to store all your pricing
    obj["hour_detail"] = []
    for pricing_instance in pricing:
        if time_instance["day_name"] == pricing_instance["day_name"]:
            obj["hour_detail"].append(pricing_instance)
    # add 'obj' after the loop
    data.append(obj)
print(data)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据所有词典中都不存在的特定键值从词典列表中获取值? - How to get value from list of dictionaries based on specific key value which does not exist in all dictionaries? 使用 python 从特定键的字典列表中获取值 - Get a value out of a list of dictionaries from a specific key with python 从字典列表中获取特定的键和值 - Getting a specific key and value from a list of dictionaries 如何根据两个列表字典中特定键的comman值从两个字典列表中获取comman数据? - How to get comman data from two list of dictionaries based on having comman values for a specific key in both list's dictionaries? 如何从词典列表中的特定键中获取唯一值? - How to get unique values from a specific key in a list of dictionaries? 从字典列表中获取特定值 - Get a specific value from a list of dictionaries 在字典列表中,将特定键的特定值从 integer 更改为字符串 - In a list of dictionaries changing a specific value of a specific key from integer to string 如何仅基于特定键返回字典列表和对象列表之间的差异:仅值 - How do I return the difference between a list of dictionaries and a list of objects based on a specific key:value only 使用对特定键值的理解从字典列表中创建一个列表 - Create a list from a list of dictionaries using comprehension for specific key value 根据特定键合并两个词典列表 - Merge two list of dictionaries based on specific key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM