首页 > 解决方案 > 如何结束周长计算循环

问题描述

我正在学习 python,并希望您在循环方面提供帮助。我想计算一组坐标的周长。在第一部分中,我能够读取文件并分离坐标。但是在第二部分中,我创建了一个循环来计算周长

我的循环一直在旋转,我不知道如何摆脱它。我正在考虑一个为期 3 天的解决方案,但我想不出办法。

import math
#import matplotlib.pyplot as plt

file = open('lagos.mif', 'rt')


linha = file.readline()

while linha != '':
    if linha.find('Region') >= 0:
        x=[]
        y=[]
        print('Found Region')
        linha = file.readline()
        n_coords = int(linha)
        print(n_coords, 'coordenadas')
        for i in range(0,n_coords):
            xy=file.readline()
            xy=xy.split(' ')
            x.append(float(xy[0]))
            y.append(float(xy[1]))
        print(x,y)
#        plt.plot(x,y)
    linha=file.readline()

file.close()

# perimeter calc

for p in range(x):
    perimetro = 0
    for k in range(0,len(x)-1):
        perimetro += sqrt((x1 - x2)**2+(y1 - y2)**2)
    perimetro += sqrt((x1 - x2)**2+(y1 - y2)**2)
return perimetro_calc

在这部分,我现在有以下错误:

 29     --->30 for p in range(x):  
 31     perimetro = 0  
 32     for k in range(0,len(x)-1):

TypeError:“列表”对象不能解释为整数

文件示例:

Version 300
Charset "Neutral"
Delimiter ","
CoordSys Earth Projection 8, 104, "m", -57, 0, 0.9996, 500000, 10000000
Columns 1
  id Integer
Data
Region 1
  43
420545.472666406 8039109.84952219
421826.398765775 8039269.96233147
423070.419882921 8039371.70593604
424071.154912852 8039956.97693793
424213.195190207 8041682.44202806
424189.618904849 8043000.27099869
424331.189696241 8044861.39502417
424847.302800208 8045948.6806605
425902.406388031 8047008.87942035
426457.319712984 8047689.25885442
427082.243442439 8047013.2096938
427529.077254886 8046762.87928647
427635.469952927 8045600.40701093
427713.356257689 8044612.25919036
427710.970600776 8042703.22278368
428827.397980469 8042183.94442967
428885.712577751 8041457.36532177
428712.158074393 8040642.74438557
428713.750392636 8040196.98677688
428790.535637652 8039499.54421983
429266.107391093 8038948.87748107
428886.372820426 8038666.50182742
428356.50566593 8038819.6532703
427844.921957098 8039050.38138099
426759.701456555 8038697.56507362
426270.149902553 8037988.34897003
426357.949286532 8036845.18533305
426595.727738791 8035314.95050044
427213.511210837 8033950.84682312
427658.958997061 8031452.29851439
427568.959170841 8030676.72149069
426558.392558189 8030343.52684403
426113.960848383 8030041.467247
424425.528589826 8029792.84179241
422337.097124758 8029775.08657013
421129.4216379 8030022.2823148
420041.446991519 8030502.46688886
419081.519814498 8031438.58296701
418928.484509449 8032542.70761832
418628.636701379 8033219.83644052
418634.787689624 8033985.43249631
419678.594591222 8037604.31748276
420545.472666406 8039109.84952219
    Pen (1,2,0)
    Brush (1,0,16777215)

标签: pythonpython-3.x

解决方案


代码重写以处理具有多个区域的文件

from math import sqrt

def get_next_region(file_handle):
    '''
        Get next region of file
    '''
    region = None
    for line in file_handle:
        if line.find('Region') != -1:
            return line.strip()
    return None  # No region found
        
def get_coords(file_handle):
    '''
        Gets the coordinates from the file
    '''
    # Previous processing by get_next_region has positioned
    # to line with coordinates
    n_coords = int(file_handle.readline())

    # Get coordinates for region
    return [[float(v) for v in file.readline().split()] for _ in range(n_coords)]

def calc_perimeter(coords):
    '''
        Finds perimeter by summing distance between successive
        pairs of coordinates
    '''
    def distance(point1, point2):
        ' Distance between two points'
        diffx = point1[0] - point2[0]
        diffy = point1[1] - point2[1]
        return sqrt(diffx*diffx + diffy*diffy)
    

    # find distance between pairs of points 0th & 1st, 1st & 2nd, ...
    perimetro = sum(distance(point1, point2) for point1, point2 in zip(coords[1:], coords))
    perimetro += distance(coords[0], coords[-1])
       
    return perimetro/1000.0  # Return perimeter in km


# Open file and get file handle
with open('lagos.txt', 'rt') as file:
    while True:
        # Postion file handle to next region
        region = get_next_region(file)
        if region:
            print(f'{region} - ', end = '')
            coords = get_coords(file)
            perimeter = calc_perimeter(coords) # Data in file 
            print(f'Perimeter in km {perimeter:.4f}') # perimeter to 4 decimal places
        else:
            break     # No more regions found

输出

Region 1 - Perimeter in km 50.7441
Region 1 - Perimeter in km 93.4518
Region 1 - Perimeter in km 57.6532

文件 logos.txt

Version 300
Charset "Neutral"
Delimiter ","
CoordSys Earth Projection 8, 104, "m", -57, 0, 0.9996, 500000, 10000000
Columns 1
  id Integer
Data
Region 1
  43
420545.472666406 8039109.84952219
421826.398765775 8039269.96233147
423070.419882921 8039371.70593604
424071.154912852 8039956.97693793
424213.195190207 8041682.44202806
424189.618904849 8043000.27099869
424331.189696241 8044861.39502417
424847.302800208 8045948.6806605
425902.406388031 8047008.87942035
426457.319712984 8047689.25885442
427082.243442439 8047013.2096938
427529.077254886 8046762.87928647
427635.469952927 8045600.40701093
427713.356257689 8044612.25919036
427710.970600776 8042703.22278368
428827.397980469 8042183.94442967
428885.712577751 8041457.36532177
428712.158074393 8040642.74438557
428713.750392636 8040196.98677688
428790.535637652 8039499.54421983
429266.107391093 8038948.87748107
428886.372820426 8038666.50182742
428356.50566593 8038819.6532703
427844.921957098 8039050.38138099
426759.701456555 8038697.56507362
426270.149902553 8037988.34897003
426357.949286532 8036845.18533305
426595.727738791 8035314.95050044
427213.511210837 8033950.84682312
427658.958997061 8031452.29851439
427568.959170841 8030676.72149069
426558.392558189 8030343.52684403
426113.960848383 8030041.467247
424425.528589826 8029792.84179241
422337.097124758 8029775.08657013
421129.4216379 8030022.2823148
420041.446991519 8030502.46688886
419081.519814498 8031438.58296701
418928.484509449 8032542.70761832
418628.636701379 8033219.83644052
418634.787689624 8033985.43249631
419678.594591222 8037604.31748276
420545.472666406 8039109.84952219
    Pen (1,2,0)
    Brush (1,0,16777215)
Region 1
  43
428278.442011525 8051122.12663314
427158.512297851 8052571.17187983
426781.461862412 8054071.31422586
425846.191254006 8055763.14382555
424723.483994769 8057841.7214055
422398.245561306 8058414.13281051
420497.337332881 8057389.56065025
418036.174719448 8057089.07614676
415757.461437007 8057612.47857374
413568.841521035 8058765.69697987
410307.799963168 8060688.9459219
408352.1881786 8061552.03475206
405831.962996648 8063865.45065155
404569.577163334 8065457.97986822
403022.994680378 8068114.71378939
402543.938681607 8071067.1270885
403423.608501578 8071846.33549811
410404.530022026 8070328.36147779
413098.244486987 8070969.75507598
415696.916124485 8072143.18673907
417134.461480857 8073069.38861636
419408.898892852 8074047.25304739
420803.447996498 8074149.60522656
422528.292337933 8072993.80147442
422862.958487958 8070573.30136103
424638.19250452 8068255.07399672
425942.041192885 8067630.22202076
427987.948844169 8067443.85824238
429946.044691733 8065755.50010013
430183.28149781 8064351.70321598
430193.156035073 8061494.0630704
430712.620338642 8059074.09135658
431598.525708581 8058156.84737197
433594.550181964 8058696.30968111
434290.677776021 8058940.76593979
434296.363744856 8057197.1242327
434255.442779213 8055501.76265744
433936.3128535 8053660.18004762
433243.753686335 8052398.57354413
431016.972989158 8051616.08062383
430225.219635944 8052243.00815511
430041.627065206 8051612.70595139
428278.442011525 8051122.12663314
    Pen (1,2,0)
    Brush (1,0,16777215)
Region 1
  18
437525.268116076 7991091.90929183
439184.27654557 7993615.92502315
437505.987335583 7997098.11204173
436565.172224574 8001551.31974644
435440.528615538 8005422.62589162
437290.88844709 8006397.3805821
439145.038225233 8006209.47949542
441565.6115818 8002535.65016431
443981.398094189 8000217.75532802
445662.004472732 7995572.56975965
446592.505959958 7994025.16264799
447897.97635515 7990734.9911962
448275.093417121 7988217.27398417
446432.39906337 7984918.5395044
443656.860344446 7984329.41398443
440694.560280707 7984320.56791064
438654.290713961 7985476.72236234
437525.268116076 7991091.90929183
    Pen (1,2,0)
    Brush (1,0,16777215)

推荐阅读