来自cs的Python方法

2024-06-07 02:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个任务,在这个任务中,我使用.csv中的行创建城市的“实例”,然后在计算距离和人口变化的方法中使用这些实例。创建实例很好(使用下面的步骤1-4),直到我尝试调用printDistance:

##Step 1. Open and read CityPop.csv
with open('CityPop.csv', 'r', newline='') as f:
try:
    reader = csv.DictReader(f)
    ##Step 2. Create "City" class
    class City:
        ##Step 3. Use _init method to assign attribute values
        def __init__(self, row, header):
            self.__dict__ = dict(zip(header, row))

            ##Step 4. Create "Cities" list
            data = list(csv.reader(open('CityPop.csv')))
            instances = [City(i, data[0]) for i in data[1:]]

            ##Step 5. Create printDistance method within "Cities" class  
            def printDistance(self, othercity, instances):
                dist=math.acos((math.sin(math.radians(self.lat)))*(math.sin(math.radians(othercity.lat)))+(math.cos(math.radians(self.lat)))*(math.cos(math.radians(othercity.lat)))*(math.cos(math.radians(self.lon-othercity.lon)))) * 6300 (self.lat, self.lon, othercity.lat, othercity.lon)

当我在shell中输入instances[0].printDistance(instances1)时,得到错误:

 `NameError: name 'instances' is not defined`

这是压痕问题吗?我应该从代码中调用函数,而不是从shell中调用吗?你知道吗

Example of how data is stored in csv


Tags: csvinstances实例selfcitystepcreatemath
2条回答

这与其说是一个缩进问题,不如说是一个一般的代码结构问题。你筑巢太多了:

  • 所有的实际工作都在一条难以置信的长线上(有错误)
  • 函数内部(正确)printDistance
  • 构造函数内部__init__
  • 类定义内部(正确地)City
  • try块的内部
  • with块内部

我想这就是你想要做的:

  • 创建一个类城市,它可以打印自己到其他城市的距离
  • 从一个既有距离又有人口的.csv文件中生成这些城市对象的列表(您可能应该提供一个数据示例)
  • 以一种容错和干净的方式执行(因此是trywith

你的instances不起作用的原因是,不像你想的那样,它可能没有被正确地创建,或者至少没有在正确的上下文中创建。而且由于所有的嵌套,您肯定无法在CLI上使用它。你知道吗

你的代码中有很多明显的错误:

  • 最后一行末尾的(self.lat, self.lon, othercity.lat, othercity.lon)是什么?你知道吗
  • 你为什么要打开文件看两遍?你甚至没有使用第一个reader
  • 很明显,您将.csv中的列标题指定为对象属性,但拼写错误(lat而不是latitudelon而不是longitude

它看起来有点像在不同地方发现的许多代码被粘贴到一个块中-这就是清理时的样子:

import csv
import math


class City:
    def print_distance(self, other_city):
        print(f'{self.city} to {other_city.city}')
        # what a mess...
        print(math.acos(
            (math.sin(math.radians(float(self.latitude)))) * (math.sin(math.radians(float(other_city.latitude)))) + (
                math.cos(math.radians(float(self.latitude)))) * (math.cos(math.radians(float(other_city.latitude)))) * (
                math.cos(math.radians(float(self.longitude) - float(other_city.longitude))))) * 6300)

    def __init__(self, values, attribute_names):
        # this is *nasty* - much better to add the attributes explicitly, but left as original
        # also, note that you're reading strings and floats here, but they are all stored as str
        self.__dict__ = dict(zip(attribute_names, values))


with open('CityPop.csv', 'r', newline='') as f:
    try:
        reader = csv.reader(f)
        header = next(reader)
        cities = [City(row, header) for row in reader]

        for city_1 in cities:
            for city_2 in cities:
                city_1.print_distance(city_2)
    except Exception as e:
        print(f'Apparently were doing something with this error: {e}')

注意print_distance现在是City的一个方法,它在cities中的City的每个实例上被调用(这就是我将instances重命名为的)。你知道吗

现在,如果你真的在努力,这就更有意义了:

import csv
import math


class City:
    def print_distance(self, other_city):
        print(f'{self.name} to {other_city.name}')
        # not a lot better, but some at least
        print(
            math.acos(
                math.sin(math.radians(self.lat)) *
                math.sin(math.radians(other_city.lat))
                +
                math.cos(math.radians(self.lat)) *
                math.cos(math.radians(other_city.lat)) *
                math.cos(math.radians(self.lon - other_city.lon))
            ) * 6300
        )

    def __init__(self, lat, lon, name):
        self.lat = float(lat)
        self.lon = float(lon)
        self.name = str(name)


try:
    with open('CityPop.csv', 'r', newline='') as f:
        reader = csv.reader(f)
        header = next(reader)
        cities = [City(lat=row[1], lon=row[2], name=row[4]) for row in reader]

        for city_1 in cities:
            for city_2 in cities:
                city_1.print_distance(city_2)
except FileNotFoundError:
    print(f'Could not find the input file.')

请注意清理的计算、捕获可能发生的错误(在with块中有try)和一个适当的构造函数,该构造函数用正确的类型分配它所需的内容,同时读取器决定哪些字段放在哪里。你知道吗

最后,作为一个额外的好处:没有人应该写这样的距离计算。有很多图书馆在这方面做得更好,比如GeoPy。你所需要做的就是pip install geopy得到它,然后你就可以使用这个:

import csv
import geopy.distance


class City:
    def calc_distance(self, other_city):
        return geopy.distance.geodesic(
            (self.lat, self.lon), 
            (other_city.lat, other_city.lon)
        ).km

    def __init__(self, lat, lon, name):
        self.lat = float(lat)
        self.lon = float(lon)
        self.name = str(name)


try:
    with open('CityPop.csv', 'r', newline='') as f:
        reader = csv.reader(f)
        header = next(reader)
        cities = [City(lat=row[1], lon=row[2], name=row[4]) for row in reader]

        for city_1 in cities:
            for city_2 in cities:
                print(city_1.calc_distance(city_2))
except FileNotFoundError:
    print(f'Could not find the input file.')

注意,我也将print移出了方法,因为在对象中计算并在对象外打印更有意义。所有这一切的好处是,现在的计算使用一个适当的测地线(WGS-84)来做计算和数学错误的几率大大降低。如果您必须使用一个简单的球体,库中也有相应的函数。你知道吗

嵌套函数不能包含self作为参数,因为它们不是成员函数。类无法将实例变量传递给它们。你实际上是在把同一个自我从父函数传递给子函数。你知道吗

另外,您不能嵌套构造函数,这仅用于初始化目的。创建一个单独的方法。你知道吗

并尝试在构造函数中创建实例变量,这就是init的目的!你知道吗

self.instances = [self.getInstance(i, data[0]) for i in data[1:]]

同时为实例化创建单独的函数

@classmethod
def getInstance(cls,d1,d2):
    return cls(d1,d2)

相关问题 更多 >