简体   繁体   English

从Python类中的2D数组返回值

[英]Return value from 2D array in Python class

I'll frame this in the fact that I'm kind of a newbie to Python and I'm taking my first stab at writing a class. 我会以我是Python的新手的事实为框架,而我正在尝试编写类。 What I'd like to achieve is for the class to open a text file and return a value from the file based on user input. 我要实现的是该类打开一个文本文件,并根据用户输入从文件中返回一个值。 The text file contains information in a 2D array like this: 文本文件包含二维数组中的信息,如下所示:

 20,21,22
 23,24,25
 26,27,28

The value is retrieved from this text file and assigned to a new variable, so that the variable can be printed and also used later in a calculation. 从该文本文件中检索该值并将其分配给新变量,以便可以打印该变量,并在以后的计算中使用该变量。 I've been able to do this without difficulty outside of a class, but getting this to work in a class has been frustrating. 我可以在课堂外毫无困难地做到这一点,但是让它在课堂上发挥作用却令人沮丧。 Here's the code I have so far in Python 2.7: 这是我到目前为止在Python 2.7中拥有的代码:

im_new = []

class Read(object):
    def __init__(self, row, col, newvar):
        self.row = row
        self.col = col
        self.newvar = newvar

        display_list = []
        self.newvar = []

        with open("Array.txt", "r") as data_file:
            for line in data_file:
                display_list.append(line.strip().split(','))

    def __str__(self):
        self.newvar = (display_list[self.row][self.col])
        return self.newvar

immem = Read( 1, 1, im_new)


print "OUTPUT: ", im_new

Ideally, I'd get "OUTPUT: 24", but instead I get "OUTPUT: []". 理想情况下,我会得到“ OUTPUT:24”,但是我会得到“ OUTPUT:[]”。 I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 Any help would be appreciated. 任何帮助,将不胜感激。

First, you're mixing up two of your variables. 首先,您要混合两个变量。 im_new is an empty list; im_new是一个空列表; immem is a Read instance. immem是一个Read实例。 Change the last line to print 'OUTPUT:', immem . 更改最后一行以print 'OUTPUT:', immem

When you fix that, you'll get another problem: a NameError on display_list . 解决此问题时,您将遇到另一个问题: display_list上的NameError This is because you forgot the self. 这是因为您忘记了self. on display_list , but in your __init__ method and in your __str__ method. display_list ,但在__init__方法和__str__方法中。 (I assume you know what self. means, because you use it correctly for all of your other attributes.) (我假设您知道self.含义。因为您将其正确地用于所有其他属性。)

As a side note, it's a little weird to override self.newvar every time you get the __str__ of your object. 附带说明一下,每次获取对象的self.newvar都覆盖self.newvar __str__ There's really no reason for this attribute to exist, at least in the code you've shown. 至少在您显示的代码中,确实没有理由存在此属性。 So you could just do this: 因此,您可以这样做:

class Read(object):
    def __init__(self, row, col):
        self.row = row
        self.col = col

        self.display_list = []

        with open("Array.txt", "r") as data_file:
            for line in data_file:
                self.display_list.append(line.strip().split(','))

    def __str__(self):
        return self.display_list[self.row][self.col]

immem = Read(1, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM