我有一个键索引,颜色代码和颜色名称的元组列表,如下所示:
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]
,因为如果out of index
会引发错误colorIndex='99'
。
我尝试过:
BackgroundColor = BG_MENU_THEME[row[0]] for row in BG_MENU_THEME if row[0]==ColorIndex
得到SyntaxError: invalid syntax
请帮忙给我建议吗?谢谢
答案 0 :(得分:3)
使用带有next
的生成器:
BackgroundColor = next(color[1] for color in BG_MENU_THEME if color[0] == colorIndex)
答案 1 :(得分:1)
使用字典也可以:
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
保留为字典,这样就不需要转换阶段了。
答案 2 :(得分:0)
Proxy p = new Proxy();
p.setHttpProxy("in-pr.oxylabs.io:20000");
p.setSslProxy("in-pr.oxylabs.io:20000");
System.setProperty("webdriver.chrome.driver","D:\\Love_Testing\\Senium_Naveen\\chrome Driver\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
// options.addArguments("--incognito");
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(ChromeOptions.CAPABILITY,options);
dc.setCapability("proxy",p);
WebDriver driver = new ChromeDriver(dc);
driver.get("https://www.google.com");
或简称: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]
。如果是,则打印该元组的第二个值。
答案 3 :(得分:0)
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
答案 4 :(得分:0)
尝试以下代码:
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])