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

时间:2019-10-25 17:35:45

标签: python dictionary

我只想选择一组满足我条件的元素(例如,属于菜单no1的元素),然后将这些元素附加到新字典中。每组元素(例如1号菜单)都有开始键-'Miniwok_Stall_Menu1'和结束键-'remark:@Miniwok_stall_Menu1'
此处的字典:https://pastebin.com/viDw1snv
我目前的计划是:
1)查找符合条件的选定元素集:星期几-必须存在于要选择的菜单的第一个值e.g Mon in Monday,Tuesday...和用餐时间中-备注键中的值必须与time_of_meal相同,e.g 'None'
2)使用枚举获取所选元素集的开始和结束索引。 #e.g(0,4,5,9)
3)使用for循环,根据索引创建一个新的字典。 (仅包含选定的元素集)
4)预期输出是与原始格式完全相同的字典,但是仅包含选定的元素集。

#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)

问题:
1)如何获取所选元素集的结束键索引?(这是备注键的索引)一组可以比另一组包含更多元素,因此将无法进行硬编码。起始键已在selected_stalls中找到。我正在寻找(starting key of menu2, ending key of menu2, starting key of menu3, ending key of menu3)
2)如何获取包含“ remark”的键的索引以用于第二条件time_of_meal?的验证
if raw_list_stall_info[remark_key_index] == time_of_meal:

1 个答案:

答案 0 :(得分:0)

您可以将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) 循环用于字典理解:

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

这将给出输出:

time_of_meal

要找到location = {i: k for i, k in enumerate(raw_list_stallinfo.values()) if k == time_of_meal} print(location) 的位置,

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

哪个位置为:

remark

然后找到remark_location = {i: k for i, k in raw_list_stallinfo.items() if k == time_of_meal} print(remark_location) 而无需进行硬编码,可以将其与上面的位置结合使用:

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

返回完整的键和值:

let fileManager = FileManager.default

do {
    let fileURL2 = fileManager.temporaryDirectory.appendingPathComponent("\(detailedList.lname!).json")

    // Write the data out into the file
    try jsonData.write(to: fileURL2)

    // Present the save controller. We've set it to `exportToService` in order
    // to export the data -- OLD COMMENT
    let controller = UIDocumentPickerViewController(url: fileURL2, in: UIDocumentPickerMode.exportToService)
    present(controller, animated: true) {
        // Once we're done, delete the temporary file
        try? fileManager.removeItem(at: fileURL2)
    }
} catch {
    print("error creating file")
}