gpt4 book ai didi

python - 在递增的 for 循环中递减变量

转载 作者:太空宇宙 更新时间:2023-11-04 07:46:19 35 4
gpt4 key购买 nike

在我的 python 脚本中,我正在遍历从索引 9 开始的列表 (headerRow)。我想检查它是否已经在数据库中,如果没有,则将其添加到具有自动增强功能的数据库中首要的关键。然后我想再次通过循环发送它以检索它的主键。

for i in range (9, len(headerRow)):
# Attempt to retrieve an entry's corresponding primary key.
row = cursor.fetchone()
print i

if row == None: # New Entry
# Add entry to database
print "New Entry Added!"
i -= 1 # This was done so that it reiterates through and can get the PK next time.
print i

else: # Entry already exists
print "Existing Entry"
qpID = row[0]
# ...

这是我的脚本的输出:

9
New Question Added!
8
10
New Question Added!
9
11

如您所见,我的问题是 range() 不关心 i 的现有值是多少。执行我想做的事情的首选 Python 方法是什么?

提前致谢

迈克

最佳答案

为什么不使用 while 循环?

i=9
while (i<len(headerRow)):
# Attempt to retrieve an entry's corresponding primary key.
row = cursor.fetchone()

if row == None: # New Entry
# Add entry to database
print "New Entry Added!"
else: # Entry already exists
print "Existing Entry"
qpID = row[0]
i += 1
# ...

关于python - 在递增的 for 循环中递减变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6861579/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com