【问题标题】:Python Defining Lists in ListsPython 在列表中定义列表
【发布时间】:2015-08-12 19:19:02
【问题描述】:

我正在尝试定义一个表颜色,其中包含各种其他表,这些表包含表示颜色的元组。

title_label = [
text = (236, 218, 51),
background = (125, 142, 246)
],
start_button = [
text = (32, 40, 145),
background = (236, 235, 136),
pressed = (44, 51, 112)
],
quit_button = [
text = (166, 21, 13),
background = (48, 61, 188),
pressed = (31, 40, 129)
]

但是,这会产生无效的语法错误。这是为什么呢?

【问题讨论】:

标签: python list tuples


【解决方案1】:

列表不采用名称-值对。您可能需要在这里使用字典:

definitions = {
    'title_label': {
        'text': (236, 218, 51),
        'background': (125, 142, 246)
    },
    'start_button': {
        'text': (32, 40, 145),
        'background': (236, 235, 136),
        'pressed': (44, 51, 112)
    },
    'quit_button': {
        'text': (166, 21, 13),
        'background': (48, 61, 188),
        'pressed': (31, 40, 129)
    }
}

我不确定你在哪里找到了你的语法,但它不是有效的 Python。 Python 列表,使用[...] 方括号,只能采用一系列单独的 Python 表达式:

some_list = ['one object', {'dictionary': 'value'}, ['another', 'list']]

请参阅 Python 教程的Data structures section

【讨论】:

    猜你喜欢
    • 2015-02-09
    • 1970-01-01
    • 2015-03-10
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2012-08-04
    相关资源
    最近更新 更多