繁体   English   中英

如何从python中的元组列表中获取Value?

[英]How to get Value out of list of tuple in python?

我有一个键索引,颜色代码和颜色名称的元组列表,如下所示:

BG_MENU_THEME = [
                    ('1','#031B4D','Blue Ocean'),
                    ('2','#303E4D','Grayish blue'),
                    ('3','#062847','Ghibli Ocean  Drak 1'),
                    ('4','#00122e','Ghibli Ocean  Drak 2'),
                    ('4','#00122e','Ghibli Ocean  Drak 2'),
                    ('5','#115478','Ghibli Ocean - Ligit'),
                    ('6','#243447','Twitterish Color'),
                    ('7','#152324','Dark Forrest'),
                    ('8','#11202F','Dark Blue Original'),
                    ('99','','_None')
                ] 

给定colorIndext='8' ,我如何获得颜色代码'#11202F' 更不用说BG_MENU_THEME[8][1]因为如果colorIndex='99' ,它将引起out of index错误。

我试过了:

BackgroundColor     = BG_MENU_THEME[row[0]] for row in BG_MENU_THEME if row[0]==ColorIndex

并得到了SyntaxError: invalid syntax

请对此提供意见吗? 谢谢

next一起使用生成器:

BackgroundColor = next(color[1] for color in BG_MENU_THEME if color[0] == colorIndex)

使用字典也可以:

BG_MENU_THEME = [
                    ('1','#031B4D','Blue Ocean'),
                    ('2','#303E4D','Grayish blue'),
                    ('3','#062847','Ghibli Ocean  Drak 1'),
                    ('4','#00122e','Ghibli Ocean  Drak 2'),
                    ('4','#00122e','Ghibli Ocean  Drak 2'),
                    ('5','#115478','Ghibli Ocean - Ligit'),
                    ('6','#243447','Twitterish Color'),
                    ('7','#152324','Dark Forrest'),
                    ('8','#11202F','Dark Blue Original'),
                    ('99','','_None')
                ]

d = {key: code for key, code, color in BG_MENU_THEME}

BackgroundColor = d['8']

print(BackgroundColor)
# #11202F

您也可以事先将BG_MENU_THEME保留为词典,这样就不需要转换阶段了。

for tup in BG_MENU_THEME:
    if tup[0] == "8":
        print (tup[1]) # background color.

或简称: [tup[1] for tup in BG_MENU_THEME if tup[0] == "8"][0]如果您真的想使用列表[tup[1] for tup in BG_MENU_THEME if tup[0] == "8"][0] 当您实际上没有使用列表推导时,您正在使用语法来实现列表推导。 这就是为什么它会引发错误。

遍历每个元组,并检查元组中的第一个值是否为"8" 如果是,则打印该元组的第二个值。

BG_MENU_THEME = [
                    ('1','#031B4D','Blue Ocean'),
                    ('2','#303E4D','Grayish blue'),
                    ('3','#062847','Ghibli Ocean  Drak 1'),
                    ('4','#00122e','Ghibli Ocean  Drak 2'),
                    ('4','#00122e','Ghibli Ocean  Drak 2'),
                    ('5','#115478','Ghibli Ocean - Ligit'),
                    ('6','#243447','Twitterish Color'),
                    ('7','#152324','Dark Forrest'),
                    ('8','#11202F','Dark Blue Original'),
                    ('99','','_None')
]

BackgroundColor = '' # give it a default value

ColorIndex = '8'
res  = [row[1] for row in BG_MENU_THEME if row[0]==ColorIndex and row[1]]

if len(res) == 1:
    BackgroundColor = res.pop()

print(BackgroundColor)
# #11202F

试试这个代码:

BG_MENU_THEME = [
                ('1','#031B4D','Blue Ocean'),
                ('2','#303E4D','Grayish blue'),
                ('3','#062847','Ghibli Ocean  Drak 1'),
                ('4','#00122e','Ghibli Ocean  Drak 2'),
                ('4','#00122e','Ghibli Ocean  Drak 2'),
                ('5','#115478','Ghibli Ocean - Ligit'),
                ('6','#243447','Twitterish Color'),
                ('7','#152324','Dark Forrest'),
                ('8','#11202F','Dark Blue Original'),
                ('99','','_None')
            ]
colorId = '8'
result = None
for data in BG_MENU_THEME:
   if data[0] == colorId:
      result = data[1]
if result:
   print(result)
else:
   print(BG_MENU_THEME[-1][1])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM