如何从对象创建列表

我有一个清单程序,它从 excel 文件中读取列表,然后创建一个类。我想覆盖列表中对象的新值,然后将列表导出到 excel。在这里,我从 excel 文件中导入列表


import pandas as pd

df = pd.read_excel('stock.xlsx')


inventory_code = df['code'].tolist()

inventory_name= df['name'].tolist()

inventory_amount = df['amount'].tolist()

inventory_unit = df["birim"].tolist()

inventory_location=df["location"].tolist()

然后我创建类


class Inventory:

    def __init__(self,i_code,i_amount,i_unit,i_location):

        self.i_code=i_code

        self.i_amount=i_amount

        self.i_unit=i_unit

        self.i_location=i_location

    def info(self):

        print("Product code:", self.i_code, "Amount:", self.i_amount, "Unit:", self.i_unit, "Place:", self.i_location)

    def add(self, add_amount):

        self.i_amount += add_amount

    def update(self):

        mylist2.append(self.i_amount)

        


for i in range(0,len(inventory_name)):

    globals()[inventory_name[i]]=Inventory(inventory_code[i],inventory_amount[i],inventory_unit[i],inventory_location[i])

现在,我想创建一个新列表 mylist2,其中包含以前的库存量和 folyo 的新值。


mylist2=[]

folyo.info()

folyo.add(999999)

folyo.update()

folyo.info()


print(mylist2)


在此代码中,输出如下:


Product code: 0 Amount: 500 Unit: m2 Place: storage_1

Product code: 0 Amount: 1000499 Unit: m2 Place: storage_1

[1000499]

但是,我的更新命令仅保存提到的内容,在本例中为 amount folyo。我想保存所有对象。我该如何解决这个问题?


我认为只更新相关的东西可以是一个解决方案。但是,我的解决方案只给出了一个空列表。


    def update(self):

        for i in range(0, len(mylist2)):

            if inventory_code[i] == self.i_code:

                mylist2[i]= self.i_amount

            else:

                pass


芜湖不芜
浏览 107回答 2
2回答

一只萌萌小番薯

解决方法如下:import pandas as pddf = pd.read_excel('stock.xlsx')inventory_code = df['code'].tolist()inventory_name= df['name'].tolist()inventory_amount = df['amount'].tolist()inventory_unit = df["birim"].tolist()inventory_location=df["location"].tolist()class Inventory:    def __init__(self,i_code,i_amount,i_unit,i_location):        self.i_code=i_code        self.i_amount=i_amount        self.i_unit=i_unit        self.i_location=i_location    def info(self):        print("Product code:", self.i_code, "Amount:", self.i_amount, "Unit:", self.i_unit, "Place:", self.i_location)    def add(self, add_amount):        self.i_amount += add_amount    def update(self):        for i in range(0, len(inventory_name)):            if inventory_code[i] == self.i_code:                mylist2.append(self.i_amount)            else:                mylist2.append(inventory_amount[i])for i in range(0,len(inventory_name)):    globals()[inventory_name[i]]=Inventory(inventory_code[i],inventory_amount[i],inventory_unit[i],inventory_location[i])mylist2=[]folyo.info()folyo.add(4000)folyo.update()folyo.info()print(mylist2)print(len(inventory_amount))print(len(mylist2))

暮色呼如

解决方法如下:import pandas as pddf = pd.read_excel('stock.xlsx')inventory_code = df['code'].tolist()inventory_name= df['name'].tolist()inventory_amount = df['amount'].tolist()inventory_unit = df["birim"].tolist()inventory_location=df["location"].tolist()class Inventory:    def __init__(self,i_code,i_amount,i_unit,i_location):        self.i_code=i_code        self.i_amount=i_amount        self.i_unit=i_unit        self.i_location=i_location    def info(self):        print("Product code:", self.i_code, "Amount:", self.i_amount, "Unit:", self.i_unit, "Place:", self.i_location)    def add(self, add_amount):        self.i_amount += add_amount    def update(self):        for i in range(0, len(inventory_name)):            if inventory_code[i] == self.i_code:                mylist2.append(self.i_amount)            else:                mylist2.append(inventory_amount[i])for i in range(0,len(inventory_name)):    globals()[inventory_name[i]]=Inventory(inventory_code[i],inventory_amount[i],inventory_unit[i],inventory_location[i])mylist2=[]folyo.info()folyo.add(4000)folyo.update()folyo.info()print(mylist2)print(len(inventory_amount))print(len(mylist2))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python