python - python中for循环的问题只得到最后一项
问题描述
我是 python 的初学者,目前我正在尝试使用selenium
.
我正在尝试使用循环遍历嵌套列表,for
但总是只得到最后一个元素。任何建议为什么?
fields = [['a','b','c'],['x','y','z']]
for i in range(len(fields)):
driver.find_element_by_xpath("element").send_keys(fields[i][0],fields[i[1],fields[i][2])
driver.find_element_by_xpath("element_save").click()
#then loop and iterate through 2nd nested list
# OUTPUT = x,y,z
我希望从索引 0 开始迭代到列表的末尾。
解决方案
您不需要range(len(list_))
仅迭代索引。
通常for
会做。您还可以使用以下命令解压缩列表*
:
fields = [['a','b','c'],['x','y','z']]
len_ = len(fields)
for i in range(len_):
driver.find_element_by_xpath("element").send_keys(*fields[i])
您还可以遍历自身的值fields
:
fields = [['a','b','c'],['x','y','z']]
for field in fields:
driver.find_element_by_xpath("element").send_keys(*field)
推荐阅读
- python-3.x - Plotting Grid Search CV grid parameter results on one graph
- vue.js - Is there anyway to set a different value for the "input" than the vue variable?
- android - 在 Android 中使用 MUPDF 1.2 或更低版本创建 64 位 .so 文件
- reactjs - 这是正确的实现吗,将道具放在 this.setState 中,还请考虑我正在使用 componentDidMount()
- java - 如何将当前用户的特定数据从 firebase 显示到新活动?
- ssas - MDX 获取每个层次结构级别的维度计数
- javascript - 使用 D3.js 创建数据的同步加载并在加载后绘制
- node.js - 在`npm install`中的`node-gyp`编译模块期间获取'OSError:[Errno 2]没有这样的文件或目录:'
- python - 用numpy批量点积?
- java - Vert.x 线程命名 - “vert.x-worker-thread-...”