[英]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.