从字符串/列表创建字典的最有效方法是什么?例如,如果我有一个列表['a', 'b', 'c', 'd']
,该如何创建以列表的元素为键,索引为值的字典?以上列表的外观:{'a': 0, 'b': 1, 'c': 2, 'd': 3}
答案 0 :(得分:5)
boolean webElement = false;
logger.info("Executing waitForElementToBeVisible for page element");
try {
logger.info("Waiting for web element " + expectedElement + " with fluent wait");
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
Wait<WebDriver> wait = new
FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(45))
.pollingEvery(Duration.ofMillis(200))
.ignoring(org.openqa.selenium.NoSuchElementException.class,org.openqa.
selenium.StaleElementReferenceException.class)
.ignoring(org.openqa.selenium.ElementNotVisibleException.class);
wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElement));
webElement = true;
} catch (Exception e) {
logger.error(expectedElement + " : Not found the element on Page");
webElement = false;
Assert.fail("WebElement is not visible");
} finally {
driver.manage().timeouts().implicitlyWait(TestConstant.ImplicitWaitTime, TimeUnit.SECONDS);
}
return webElement;
}
将返回元素及其索引,您可以在字典理解中使用它。
enumerate()
答案 1 :(得分:0)
您可以使用此:
lista = ['a', 'b', 'c', 'd']
dictionary = {}
n = 0
for el in lista:
dictionary[el] = n
n += 1