Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.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 类型错误:';公寓';对象不可下标_Python_Python 3.x - Fatal编程技术网

Python 类型错误:';公寓';对象不可下标

Python 类型错误:';公寓';对象不可下标,python,python-3.x,Python,Python 3.x,这就是我试图解决的问题。 创建一个名为“公寓”的类,该类具有属性flatnumber、owner name、Electric bill amount。使用def init(self):pass创建另一个类“公寓”_演示,以创建一个方法getSecondMinBill,该方法获取对象列表并将第二个最小电费作为输出 Input:3(no.of objects to be created) 1000 Hari 5000 1001 Hena 5002 1002 Harsha 5001 Output:50

这就是我试图解决的问题。 创建一个名为“公寓”的类,该类具有属性flatnumber、owner name、Electric bill amount。使用def init(self):pass创建另一个类“公寓”_演示,以创建一个方法getSecondMinBill,该方法获取对象列表并将第二个最小电费作为输出

Input:3(no.of objects to be created)
1000
Hari
5000
1001
Hena
5002
1002
Harsha
5001
Output:5001
这是我的密码

class apartment:
    def __init__(self,flat_no,owner_name,ele_bill):
        self.flatnumber=flat_no
        self.owner_name=owner_name
        self.ele_bill_amount=ele_bill

class apartment_demo(apartment):
    def __init__(self):
        pass
    def get_second_min(self):
        list=[]
        a=int(input())
        for i in range(a):
            f=int(input())
            o=str(input())
            e=int(input())
            list.append(apartment(f,o,e))
        l=len(list)
        #print(self.ele_bill_amount for i in list)
        for i in range(0,l):
            for j in range(0,l-i-1):
                if list[j][2]>list[j+1][2]:
                    temp=list[j][2]
                    list[j][2]=list[j+1][2]
                    list[j+1][2]=temp
        print(list[1][2])
        #print("Flat no:",self.flatnumber,"Owner 
        #name:",self.owner_name,"Ele.bill:",self.ele_bill_amount)

a=apartment_demo()
a.get_second_min()    
这就是我得到的错误

if list[j][2]>list[j+1][2]:
TypeError: 'apartment' object is not subscriptable

你的代码很难破译,所以我把它整理了一下,以便更可读。首先,您不应该使用限制词作为变量名,例如
List
,因为这可能会导致一些问题。接下来,不需要继承
单元
类,因为您没有从中使用任何方法。此外,您不必将名称输入强制转换为字符串,因为输入已经是字符串。最后,您查找第二大账单的方法很难阅读,因此我创建了一个不同的方法。下面是代码

    def __init__(self,flat_no,owner_name,ele_bill):
        self.flatnumber=flat_no
        self.owner_name=owner_name
        self.ele_bill_amount=ele_bill

class apartment_demo:
    def __init__(self):
        pass
    def get_second_min(self):
        obj_list=[]
        bill_list =[]
        a=int(input())
        for i in range(a):
            f=int(input())
            o=input()
            e=int(input())
            obj_list.append(apartment(f,o,e))
        for i in obj_list:
          bill_list.append(i.ele_bill_amount)
        max_bill = max(bill_list)
        while True:
          bill_list.remove(max_bill)
          if max_bill not in bill_list:
            second_largest = max(bill_list)
            print("The second largest bill is " + str(second_largest))
            break
        #print("Flat no:",self.flatnumber,"Owner 
        #name:",self.owner_name,"Ele.bill:",self.ele_bill_amount)

a=apartment_demo()
a.get_second_min()  

这里发生的事情是,我将每个对象附加到
obj_列表
,为了更清楚,我从
list
重命名了该列表。然后,我创建了一个新列表来存储每个公寓的账单,我称之为
bill\u list
。在这里,我使用了
max()
函数,该函数将一个列表作为输入,并返回该列表中的最大值。然后,我使用while循环使用
.remove(max\u bill)
删除最大的账单。然后,我使用
检查最大的账单是否仍在列表中,如果最大账单不在账单列表中:
。如果此条件为真,我只需再次调用
max()
,以返回列表中的下一个最大值。这将是你的第二大账单。如果条件为false,while循环将再次迭代并删除最大的账单。

您可以使用下面的方法,只需对代码进行最小的更改-


class apartment():
    def __init__(self,flat_no,owner_name,ele_bill):
        self.flatnumber=flat_no
        self.owner_name=owner_name
        self.ele_bill_amount=ele_bill

    def getAttr(self):
        return [self.flatnumber, self.owner_name, self.ele_bill_amount]

class apartment_demo(apartment):
    def __init__(self):
        pass
    def get_second_min(self):
        print('inside')
        list=[]
        a=int(input('Num: '))
        for i in range(a):
            f=int(input('flat no: '))
            o=str(input('owner name: '))
            e=int(input('elect bill: '))
            a = apartment(f,o,e)
            list.append(a.getAttr())
        l=len(list)
        #print(self.ele_bill_amount for i in list)
        for i in range(0,l):
            for j in range(0,l-i-1):
                if list[j][2]>list[j+1][2]:
                    temp=list[j][2]
                    list[j][2]=list[j+1][2]
                    list[j+1][2]=temp
        print(list[1][2])
        #print("Flat no:",self.flatnumber,"Owner 
        #name:",self.owner_name,"Ele.bill:",self.ele_bill_amount)

a=apartment_demo()
a.get_second_min()


list[j]
返回您的
公寓
类的实例;让我们称之为apt1。那么,当您请求apt1[2]的值时,您希望返回什么?哦,尽量不要将变量命名为与内置变量相同的名称。