将对象追加到另一个对象的列表

2024-09-30 01:37:45 发布

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

我似乎很难将对象添加到其他对象的列表中。 我会尽力解释:

我是python新手,我正在尝试学习类和对象,所以我为自己准备了一个小的exe

我的计划有三个目标:投资者、房屋和房屋;市场

在市场舱里,我只有一张房屋清单

我有一个由3名投资者组成的列表,每个人都有一个字段,这是一个房屋列表(从第一个init开始为空)

现在,我想做的是展示市场上的房屋清单(我已经做了) 用户输入他想买的房子的索引,程序将房子添加到他的房子列表中,并将其从市场上的房子列表中删除

现在我的问题似乎是,每次我选择一栋房子时,它都会将它添加到所有3位投资者,而不是特定的一位投资者,我不明白为什么(可能是因为它是ref类型,但我不知道如何克服它:/)

代码:

class Investor:
    def __init__(self, name, budget, houses):
        self.name = name
        self.budget = budget
        self.houses = houses

    def BuyHouse(self, market, houseIndex):
        self.budget -= market.availableHouses(houseIndex).price
        self.houses.append(market.availableHouses[houseIndex])  ## i believe this to be the problem. 
        print(f'Budget left: {self.budget}$')    

    def PrintAllInfo(self): # prints all info for houses.
        counter = 1
        for h in self.houses:
            print(f'{counter}) The House is in: {h.location}\n   The House price is: {h.price}$\n   The House size is: {h.size} sqft')
            counter += 1

class House:
    def __init__(self, location, price, length, width):
        self.location = location
        self.price = price
        self.width = width
        self.length = length

    def PrintThisHouse(self):
        print(f'{self.location}, Price - {self.price}$')

class Market:
    def __init__(self, availableHouses):
        self.availableHouses = availableHouses

    def PrintAllHouses(self):
        print('\nALL ABAILABLE PROPERTIES:\n--------------------------')
        counter = 1
        for h in self.availableHouses:
            print(str(counter) + ' - ', end='')
            counter += 1
            h.PrintThisHouse()

# main #
invHouses = []
investors = [ Investor('Bot1', 100000000, invHouses),
              Investor('Bot2', 100000000, invHouses),
              Investor('Bot3', 100000000, invHouses) ]
# init Houses:
houses = [ House("LA, California",450000,20,10),
           House("IL, Tel Aviv",1000000,5,5),
           House("Rus, Moscow",40000,20,10),
           House("Switz, Zr",125000,7,9),
           House("Fr, Paris",225000,15,4),
           House("Eg, Cairo",75000,15,15),
           House("Pt, Lisbon",100000,10,10),
           House("Ge, Batumi",75000,14,6),
           House("In, New Delhi",50000,20,20),
           House("Ca, Montreal",500000,30,35),
           House("Cambodia, Phnom Pen",15000,9,9),
           House("Uk, London",1000000,10,10) ]

#init Market:
mrkt = Market(houses)


# runs 3 times 1 time for each investor to buy one house.
for inv in investors: # run for all investors
    mrkt.PrintAllHouses() # a func that prints all the houses that are on the market
    selectHouse = (int(input('Enter the house you want to buy: ')) - 1)
    inv.BuyHouse(mrkt, selectHouse)


#after each investor bought a house i want to print for each investor his house(s) in the list
# problem is that now it prints all 3 houses for each one and i dont know why :(
for inv in investors:
    print(f'\nProperties of {inv.name}:')
    inv.PrintAllInfo()
    print('\n**************************')

非常感谢你们的帮助,谢谢大家


Tags: inself列表forinitdefcounterprice
2条回答

我认为问题在于,你正在向所有投资者传递相同的投资公司名单。尝试将self.house初始化为investor初始中的空列表

invHouses = []
investors = [ Investor('Bot1', 100000000, invHouses),
              Investor('Bot2', 100000000, invHouses),
              Investor('Bot3', 100000000, invHouses) ]

您有一个由invHouses创建的列表对象。你正在把同样的目标传递给所有的投资者。因此,如果你通过一个投资者将一所房子添加到该列表中,那么所有其他投资者也会将该房子添加到他们的列表中(因为它是同一个列表)

investors = [ Investor('Bot1', 100000000, []),
              Investor('Bot2', 100000000, []),
              Investor('Bot3', 100000000, []) ]

而是将不同的对象传递给每个投资者

相关问题 更多 >

    热门问题