Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 递减递增for循环中的变量_Python - Fatal编程技术网

Python 递减递增for循环中的变量

Python 递减递增for循环中的变量,python,Python,在我的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

在我的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方式是什么

提前感谢,


迈克

为什么不在循环时使用

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
        # ...
i=9

while(i为什么不使用
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
        # ...
i=9

然而(我非常讨厌手动更改索引变量,这让我想对此发表评论……) 把它改成在同一个迭代中做这两件事怎么样? 代码看起来有点奇怪,但你明白了

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!"
        row = cursor.fetchone() # re-fetch to get the PK

    # Entry will exist now
    print "Getting Existing Entry"

    qpID = row[0]
    # ...
为了解释为什么递减“i”不起作用:


for循环实际上并没有增加变量。它只是从您给它的序列(由range函数生成)中选取下一个值。因此,如果序列是
[9,10,11,12]
,它将依次选取这些值。变量“i”将获得下一个值,而上一个值将被丢弃。任何增量或减量都不会影响这一点。

我非常讨厌手动更改索引变量,因此我想对此发表评论……) 把它改成在同一个迭代中做这两件事怎么样? 代码看起来有点奇怪,但你明白了

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!"
        row = cursor.fetchone() # re-fetch to get the PK

    # Entry will exist now
    print "Getting Existing Entry"

    qpID = row[0]
    # ...
为了解释为什么递减“i”不起作用:


for循环实际上并没有增加变量。它只是从您给定的序列(由range函数生成)中选择下一个值。因此,如果序列是
[9,10,11,12]
,它将按顺序选择这些序列。变量“i”将获得下一个值,上一个值将被丢弃。没有任何增量或减量会影响这一点。

它工作正常且简单。我喜欢。现在更好的问题是为什么使用for循环在我的脑海中根深蒂固。谢谢。它很有效而且很简单。我喜欢。现在更好的问题是为什么使用for循环在我的脑海中根深蒂固。谢谢