1

谁能解释这段代码在做什么

dns = find_all(S("some value"))

index = [dns.index(x) for x in dns if x.web_element.get_attribute("name") == "some value"]

任何帮助表示赞赏。谢谢

4

1 回答 1

1

变量 dns 应该是一个可迭代的......意味着它是一个列表、元组等。

代码遍历dns列表,列表中的每一项都放在变量x中。x 是一个具有 web_element.get_attribute 方法的对象。字符串“name”被传递给该对象。如果返回值是“某个值”,那么 dns.index(x) 的结果将放在变量 someValue 中。然后将 someValue 添加到 newList。

在我看来,代码正在创建满足 if 语句建立的标准的 dns 列表对象的索引列表。下面的代码对于 python 列表理解的新手来说可能更清楚,但做同样的事情。希望这可以帮助。

newList = [] # empty list
for x in dns:
    if x.web_element.get_attribute("name") == "some value"
        someValue = dns.index(x)
        newList.append(someValue)
于 2016-11-04T19:21:52.050 回答