python跳过for循环

Posted

技术标签:

【中文标题】python跳过for循环【英文标题】:python skipping a for loop 【发布时间】:2015-07-28 23:58:33 【问题描述】:

我正在编写代码查找经纬度,并计算一个点的一定半径内的距离,并将两个文件分开。

对于前 5 次迭代,程序运行良好,但之后,程序没有运行内部 for 循环。我已经逐步完成了代码,它只是跳过了for 循环。它似乎取决于我将变量 radius 设置为的内容。如果radius 更小,它将允许内部for 循环的迭代次数更少。

恐怕这可能是我在文件中读取方式的问题。我相信在第 5 次迭代之后,infile_2 是空白的,但我不知道如何修复它。

def main(): 

    global infile_1, infile_2

    ## import geocoded college dataset
    infile_2 = open("university_FIPS.csv", "r")

    ## import great_lakes_sample
    infile_1 = open("great_lakes_sample.csv", "r")
    outfile_within_bound = open("great_lakes_blood_college", "w")
    outfile_outside_bound = open("great_lakes_blood_NOcollege", "w")
    inside_buffer_count = 0
    outside_buffer_count = 0

    global lat_1_index, long_1_index, lat_2_index, long_2_index

    ## set radius to desired length (in miles)
    radius = 100



    ## when it is generalized, use this:
    ##    radius = input_buffer_radius()


    # create two subsets of blood drive data, one within
    # radius of college, one outside

    # skip header
    n_1 = 0
    for infile_1_line in infile_1:
        infile_1_line = infile_1_line.strip().replace("\"", "").split(","),
        infile_1_line = infile_1_line[0]        

        record_stored = False

        # find index of lat_2, long_2
        if( n_1 == 0 ):
            lat_2_index = infile_1_line.index( "lat" )
            long_2_index = infile_1_line.index( "long" )
            infile_1_header_list = infile_1_line

        # assign lat_2, long_2 latitude and longitude values
        lat_2 = infile_1_line[ lat_2_index ]
        long_2 = infile_1_line[ long_2_index ]

        # skip header
        if n_1 > 0:
            print( "\n\nExamining Record:", n_1 )

            try:
                lat_2 = float( lat_2 )
                long_2 = float( long_2 )
            except ValueError:
                print( "Value error, skipping record" )
                continue                        
            except TypeError:
                print("Type error, skipping record" )
                continue
            print( "Coordinates for record:", lat_2, long_2)


            # skip header
            n_2 = 0


            # WILL NOT ENTER LOOP ON THIRD ITERATION, it's dependent on radius, when radius is 100, it stops at n_1 = 6
            if ( n_1 > 0):
                print("\n\n\nbefore loop")            

            for infile_2_line in infile_2:
                infile_2_line = infile_2_line.strip().split(",")                                                        

                if ( n_2 == 0):
                    print( "in" )


                # find index of lat_1, long_1, create header list
                if( n_2 == 0 and n_1 == 1):
                    lat_1_index = infile_2_line.index("lat")
                    long_1_index = infile_2_line.index("long")
                    infile_2_header_list = infile_2_line

                    # creat headers for each outfile
                    write_to_csv( infile_1_header_list, outfile_within_bound )
                    write_to_csv( infile_1_header_list, outfile_outside_bound )


                # assign values for lat_1, long_1, after header
                if( n_2 > 0 ):
                    lat_1 = infile_2_line[ lat_1_index ]
                    long_1 = infile_2_line[ long_1_index ] 

                    try:
                        lat_1 = float( lat_1 )
                        long_1 = float( long_1 )
                        value_error = False
                    except ValueError:
                        continue
                    except TypeError:
                        continue

                    dist = haversine_distance(lat_1, long_1, lat_2, long_2)

                    if( dist <= radius ):
                        print( "\nRecord", n_1, "is",
                               dist, "miles from", lat_1, long_1)
                        write_to_csv( infile_1_line, outfile_within_bound )
                        record_stored = True
                        print( "Record stored in outfile_inside_bound." )
                        print( "Moving to next record." )
                        inside_buffer_count += 1
                        break

                n_2 += 1

            if( record_stored == False):

                print( "\nOutside buffer." )
                write_to_csv( infile_1_line, outfile_outside_bound )
                outside_buffer_count += 1
                print( "Record stored in outfile_outside_bound." )


        n_1 += 1

    print("\nRecords within buffer:", inside_buffer_count, "\nRecords outside buffer:", outside_buffer_count)
    infile_1.close()
    infile_1.close()
    outfile_within_bound.close()
    outfile_outside_bound.close()

【问题讨论】:

【参考方案1】:

直接的答案是,当您在 for x in f 样式循环中遍历文件时,python 实际上会跟踪您进入文件的距离。因此,如果您在到达断点之前对内部 for 循环进行了 10 次迭代,那么下次您尝试使用 infile_2 遍历文件时,您将在文件中开始 10 行!

听起来在您的情况下,到第三次迭代时,您已经读取了整个文件,因此在外部 for 循环的所有后续迭代中,infile_2 迭代器将仅位于文件末尾。简单的解决方法是在内部 for 循环运行之前执行 infile_2.seek(0)。这将重新定位infile_2 以再次查看文件的开头。 呼呼...

这一切都很好,但我想向你建议几件事:

    当您打开文件时使用with open("test.txt","r") as f,如this SO post 所示。这使您不必记住显式关闭文件,因为它在块末尾隐式关闭。

    通常最好将文件读入列表,进行计算,然后一次性写出所有结果。这使您的代码更有条理(也更易于阅读),并且还可以让您避免像您遇到的错误一样。

为了说明这些策略,我将如何阅读您的代码示例中的文件:

def main(): 
    global infile_1, infile_2

    with open("great_lakes_sample.csv", "r") as infile_1:
        #List comprehension to format all of the lines correctly
        infile1_lines = [line.strip().replace("\"", "").split(",") for line in infile_1] 

    with open("university_FIPS.csv", "r") as infile_2:
        #List comprehension to format all of the lines correctly
        infile2_lines = [line.strip().split(",") for line in infile_2]

    #Both files are automatically closed when their respected with blocks end.

【讨论】:

以上是关于python跳过for循环的主要内容,如果未能解决你的问题,请参考以下文章

如何有条件地跳过python中for循环中的迭代步骤数?

Python:如果条件为真,则跳过 For 循环中的迭代

如果对象为空,则跳过 for 循环

为啥 Python for 循环在遍历列表副本并进行删除时会跳过元素? [复制]

如何在python里面for循环中放了一个定时函数,当定时函数运行时跳过本次循环,执行那个循环

是否有一种 Pythonic 的方式来跳过 for 循环中的 if 语句以使我的代码运行得更快?