简体   繁体   中英

How to get Value out of list of tuple in python?

I have a list of tuple of key index, color code, and color name as like this:

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')
                ] 

Given colorIndext='8' , how can I get color code '#11202F' ? not to say BG_MENU_THEME[8][1] because it will raise an error out of index if colorIndex='99' .

I tried:

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

and got SyntaxError: invalid syntax

Please kindly help advise me on this ? Thanks

next一起使用生成器:

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

Using a dictionary also works:

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

You could also keep BG_MENU_THEME as a dictionary beforehand, then no transformation stage is needed.

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

or the short way: [tup[1] for tup in BG_MENU_THEME if tup[0] == "8"][0] if you really want to use list comprehension. You are using the syntax for a list comprehension when you are not actually using list comprehension. That's why it's raising an error.

Iterate through each tuple and check if first value in tuple is "8" . If it is print the second value of that tuple.

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

Try this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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