Python - 嵌套列表 - 有效检查

use*_*714 3 python nested-lists

我有一个嵌套列表和char变量:

colors =    [
        ("r", [255,0,0]),
        ("g", [0,255,0]),
        ("b", [0,0,255]),
        ("y", [255,0,0]),
        ("p", [255,0,255])
            ]   

char_to_check="b"
Run Code Online (Sandbox Code Playgroud)

我怎样才能(以最有效的方式)检查:
1.如果char_to_check存在于嵌套列表颜色中,则索引(r,g,b等)

2.如果存在(char_to_check),则从颜色中为此char提供值(对于示例255,0,0)
否则返回错误(任何类型)

hsp*_*her 5

只需创建一个哈希(或根据Python的字典)

colors_dict = dict(colors)

if char_to_check in colors_dict:
    rgb_values = colors_dict[char_to_check]
    # do something
Run Code Online (Sandbox Code Playgroud)