从字典中检索键和值,未找到值

huangapple go评论62阅读模式
英文:

Retrieving a key and value from a dictionary not finding value

问题

您正在尝试从给定的字典中检索特定时间段的值,但是您的代码中存在一些问题。为了正确检索"35,Piano"或其他值,您可以按照以下方式修改您的代码:

import ast

# 假设raw_data[1]包含您的字典字符串
raw_data = [
    {
        1: {'10:00 11:00': ['35,Piano']}, 2: {'10:00 11:00': ['39,Piano']}, 
        3: {'8:45 9:15': ['88,Piano'], '9:15 9:45': ['89,Piano'], '9:45 10:15': ['99,Piano']},
        4: {'9:00 9:30': ['100,Piano', '117,Piano'], '9:30 10:00': ['124,Piano'], 
            '10:00 10:30': ['125,Piano'], '10:30 11:00': ['126,Piano'], '11:00 11:30': ['127,Piano']},
        5: {'9:00 9:30': ['128,Piano'], '9:30 10:00': ['129,Piano'], '10:00 10:30': ['130,Piano'], '10:30 11:00': ['131,Piano']}
    }
]

# 假设您想要检索星期几的数据
days_in_current_timetable = [1, 2, 3, 4, 5]

for x in days_in_current_timetable:
    # 使用ast.literal_eval()将字符串转换为字典
    sterilised_list = ast.literal_eval(raw_data[x])
    print("This is The Dictionary: ", sterilised_list)
    
    for time_slot, values in sterilised_list.items():
        print("This is the key:", time_slot)
        print("This is the value:", values)

这将输出特定时间段的值,例如:"35,Piano"。请注意,我在代码中进行了一些修改,以使其更符合Python的语法规则,并检索了字典的正确元素。

英文:

I have a dictionary which looks like this:

{1: {'10:00 11:00': ['35,Piano']}, 2: {'10:00 11:00': ['39,Piano']}, 3: {'8:45 9:15': ['88,Piano'], '9:15 9:45': ['89,Piano'], '9:45 10:15': ['99,Piano']}, 4: {'9:00 9:30': ['100,Piano', '117,Piano'], '9:30 10:00': ['124,Piano'], '10:00 10:30': ['125,Piano'], '10:30 11:00': ['126,Piano'], '11:00 11:30': ['127,Piano']}, 5: {'9:00 9:30': ['128,Piano'], '9:30 10:00': ['129,Piano'], '10:00 10:30': ['130,Piano'], '10:30 11:00': ['131,Piano']}}

It is retrieved via an SQL query as a string and converted by ast.literal_eval()

I am trying to retrieve all values for each time slot, but it doesn't find it.

days_in_current_timetable = [1,2,3,4,5]

for x in days_in_current_timetable:
    
    # data is retrieved from an SQL query as a string so used ast.literal_eval() to convert
    self.sterilised_list = ast.literal_eval(raw_data[1]) 
 
    print("This is The Dictionary: ", sterilised_list)

    for slot in self.sterilised_list.values():
        for key in slot:
            print("This is the key:", key)
            print("This is the value: ", sterilised_list.get(key, "Not Found"))

Here is an example of the output:

This is The Dictionary:  {1: {'10:00 11:00': ['35,Piano']}, 2: {'10:00 11:00': ['39,Piano']}, 3: {'8:45 9:15': ['88,Piano'], '9:15 9:45': ['89,Piano'], '9:45 10:15': ['99,Piano']}, 4: {'9:00 9:30': ['100,Piano', '117,Piano'], '9:30 10:00': ['124,Piano'], '10:00 10:30': ['125,Piano'], '10:30 11:00': ['126,Piano'], '11:00 11:30': ['127,Piano']}, 5: {'9:00 9:30': ['128,Piano'], '9:30 10:00': ['129,Piano'], '10:00 10:30': ['130,Piano'], '10:30 11:00': ['131,Piano']}}
This is the key: 10:00 11:00
This is the value:  Not Found

How should I properly retrieve '35,Piano', or any of the other values?

答案1

得分: 1

  1. sterilised_list 应该是 self.sterilised_list
  2. 不要称一个字典为列表,这会造成混淆。
  3. key 不是 sterilised_list 的键,而是 slot 的键。

更改最后一行为:

print("这是值:", slot.get(key, "未找到"))

最小可复现示例(删除了与 self 相关的内容):

sterilised_dict = {1: {'10:00 11:00': ['35,Piano']}, 2: {'10:00 11:00': ['39,Piano']}, 3: {'8:45 9:15': ['88,Piano'], '9:15 9:45': ['89,Piano'], '9:45 10:15': ['99,Piano']}, 4: {'9:00 9:30': ['100,Piano', '117,Piano'], '9:30 10:00': ['124,Piano'], '10:00 10:30': ['125,Piano'], '10:30 11:00': ['126,Piano'], '11:00 11:30': ['127,Piano']}, 5: {'9:00 9:30': ['128,Piano'], '9:30 10:00': ['129,Piano'], '10:00 10:30': ['130,Piano'], '10:30 11:00': ['131,Piano']}}
print("这是字典:", sterilised_dict)
for slot in sterilised_dict.values():
    for key in slot:
        print("这是键:", key)
        print("这是值:", slot.get(key, "未找到"))
英文:
  1. sterilised_list should be self.sterilised_list
  2. don't call a dictionary a list. It's confusing
  3. key is not a key of sterilised_list, but a key of slot

Change the last line to

print("This is the value: ", slot.get(key, "Not Found"))

Minimal reproducible example (with self stuff removed):

sterilised_dict = {1: {'10:00 11:00': ['35,Piano']}, 2: {'10:00 11:00': ['39,Piano']}, 3: {'8:45 9:15': ['88,Piano'], '9:15 9:45': ['89,Piano'], '9:45 10:15': ['99,Piano']}, 4: {'9:00 9:30': ['100,Piano', '117,Piano'], '9:30 10:00': ['124,Piano'], '10:00 10:30': ['125,Piano'], '10:30 11:00': ['126,Piano'], '11:00 11:30': ['127,Piano']}, 5: {'9:00 9:30': ['128,Piano'], '9:30 10:00': ['129,Piano'], '10:00 10:30': ['130,Piano'], '10:30 11:00': ['131,Piano']}}
print("This is The Dictionary: ", sterilised_dict)
for slot in sterilised_dict.values():
    for key in slot:
        print("This is the key:", key)
        print("This is the value: ", slot.get(key, "Not Found"))

huangapple
  • 本文由 发表于 2023年2月8日 20:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定