简体   繁体   English

如何使用索引根据键和值将字典拆分为多个字典?

[英]How to split dictionary according to key and value into multiple dictionaries using indexes?

I want to select only a set of elements (eg elements belonging to menu no1) that fufils my condition, and append the elements into a new dictionary.我只想 select 满足我的条件的一组元素(例如属于菜单 no1 的元素),并将 append 元素放入一个新字典中。 Each set of elements, (eg or menu no.1) has starting key - 'Miniwok_Stall_Menu1' and ending key - 'remark:@Miniwok_stall_Menu1' .每组元素(例如或菜单号 1)都有开始键 - 'Miniwok_Stall_Menu1'和结束键 - 'remark:@Miniwok_stall_Menu1'
dictionary here: https://pastebin.com/viDw1snv这里的字典: https://pastebin.com/viDw1snv
My current plan is to:我目前的计划是:
1) Find selected set of elements that fufils the conditions: Day of week - must exist in the first value for menu to be selected eg Mon in Monday,Tuesday... and time of meal - the value in the remark key has to be the same as time_of_meal, eg 'None' 1)找到满足条件的选定元素集:星期几 - 必须存在于要选择的菜单的第一个值中, eg Mon in Monday,Tuesday...和用餐时间 - 备注键中的值必须是与 time_of_meal 相同, eg 'None'
2) Using enumerate to get start and ending indexes of selected set of elements. 2) 使用 enumerate 获取所选元素集的开始和结束索引。 #eg(0,4,5,9)
3) using a for loop, create a new dictionary based on the indexes. 3)使用for循环,根据索引创建一个新字典。 (containing only the selected set of elements) (仅包含选定的元素集)
4) expected output is a dictionary with the exact same format as the original, but with only selected set of elements. 4) 预期 output 是一个与原始格式完全相同的字典,但只有选定的元素集。

#raw_list_stallinfo is assigned to the dictionary in pastebin.
selected_stalls = []
Day_of_week = 'Mon'
time_of_meal = 'None' #None is inclusive of breakfast,lunch and dinner

for raw_list_idx, raw_list_key in enumerate(raw_list_stallinfo.keys()):
    #select the menu number based on day of week
    if Day_of_week in raw_list_stallinfo[raw_list_key]:

        #day_of_week is always found in the first index of a set of elements
        selected_stalls.append(raw_list_idx)



print(selected_stalls)

Questions:问题:
1) How to get indexs of ending key of selected set of elements?(which is the index of the remark key) One set can have more elements than the other so will not be able to hardcode. 1)如何获取所选元素集的结束键的索引?(这是remark键的索引)一组可以有比另一组更多的元素,因此无法硬编码。 The starting key is already found in selected_stalls.启动键已在 selected_stalls 中找到。 I am looking for (starting key of menu2, ending key of menu2, starting key of menu3, ending key of menu3)我在找(starting key of menu2, ending key of menu2, starting key of menu3, ending key of menu3)
2)How to get the index of the key containing 'remark' to use for validation for second condition time_of_meal? 2)如何获取包含'remark'的键的索引以用于验证第二个条件time_of_meal?
if raw_list_stall_info[remark_key_index] == time_of_meal:

You can put your for loop in dictionary comprehension:您可以将for循环放入字典理解中:

selected_stalls = []
Day_of_week = 'Mon'
time_of_meal = 'None'

raw_list_stallinfo = {'MiniWok_Stall_Menu1': 'Monday_Tuesday@Wednesday', 'Item1: Seafood HorFun@MiniWok_Stall_Menu1': '$4.50', 'Item2: Fish HorFun@MiniWok_Stall_Menu1': '$3.50', 'Item3: Chicken Horfun@MiniWok_Stall_Menu1': '$3.50', 'remark:@MiniWok_Stall_Menu1': 'None', 'MiniWok_Stall_Menu2': 'Thursday_Friday@Sunday', 'Item1: Seafood Fried Rice@MiniWok_Stall_Menu2': '$4.50', 'Item2: Salted Fish Fried Rice@MiniWok_Stall_Menu2': '$3.50', 'Item3: Chicken Fried Rice@menu2': '$3.50', 'remark:@MiniWok_Stall_Menu2': 'None', 'MiniWok_Stall_Menu3': 'Monday_Tuesday_Wednesday_Thursday_Friday@Sunday', 'Item1: Chicken Porridge@MiniWok_Stall_Menu3': '$3.00', 'Item2: Century-egg porridge@MiniWok_Stall_Menu3': '$4.00', 'remark:@MiniWok_Stall_Menu3': 'Breakfast Only'}

raw_list = {raw_list_idx: raw_list_key  for raw_list_idx, raw_list_key in enumerate(raw_list_stallinfo.keys()) if Day_of_week in raw_list_stallinfo[raw_list_key]}
selected_stalls.append(raw_list)
print(selected_stalls)

This will give the output:这将给出 output:

[{0: 'MiniWok_Stall_Menu1', 10: 'MiniWok_Stall_Menu3'}]

To find the time_of_meal location:要查找time_of_meal位置:

location = {i: k for i, k in enumerate(raw_list_stallinfo.values()) if k == time_of_meal}
print(location)

Which gives the locations as:其中给出的位置为:

{4: 'None', 9: 'None'}

Then to find the remark without hard-coding you can marry it up with the location above:然后要找到没有硬编码的remark ,您可以将其与上面的位置结合起来:

remark_location = {i: k for i, k in raw_list_stallinfo.items() if k == time_of_meal}
print(remark_location)

Which returns the full Key and Value:它返回完整的键和值:

{'remark:@MiniWok_Stall_Menu1': 'None', 'remark:@MiniWok_Stall_Menu2': 'None'}

暂无
暂无

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

相关问题 使用字典中的值字段作为键以拆分成多个字典 - Using value field in a dictionary as a key to split out into multiple dictionaries 如何快速将字典拆分成多个字典 - How to split dictionary into multiple dictionaries fast 通过字典值之一的索引将字典拆分为多个字典 - Split dictionary into multiple dictionaries by indices of one of dictionary's value 如何使用相同的键将列表拆分为多个字典? - How to split a list in to multiple dictionaries with the same key? 如何将相同的键值字典拆分成字典列表,而这些字典中的那些相等的键位于不同的列表中? - How to split same key value dictionary into a list of dictionaries with those equal keys in different list? 如何使用两个字典创建嵌套字典,其中第一个字典的值等于第二个字典的键 - How to create nested dictionary using two dictionaries in which value of first dictionary is equal to the key of second dictionary 如何将字典列表附加到字典中的键值? - How to append a list of dictionaries to the value of a key in a dictionary? 如何使用内部键:值对将字典列表转换为字典字典 - How to convert list of dictionaries into a dictionary of dictionaries by using inner key:value pair 根据键值拆分字典 - Split dictionary according to key values 如何使用我想要获取的字典的一个键/值对从字典列表中访问字典 - How to access the dictionary from a list of dictionaries using one key/value pair of the dictionary that I want to fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM