簡體   English   中英

如何從對象創建列表

[英]How to create lists from objects

我有一個庫存程序,它從 excel 文件中讀取列表,然后創建 class。 我想覆蓋列表中對象的新值,然后將列表導出到 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

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)

在這段代碼中,這里是 output:

Product code: 0 Amount: 500 Unit: m2 Place: storage_1
Product code: 0 Amount: 1000499 Unit: m2 Place: storage_1
[1000499]

但是,我的更新命令只保存提到的東西,在這種情況下是數量 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

謝謝!

解決如下:

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):
        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))

您需要將 folyo 添加到您的列表中,或者使用 Inventory object 初始化列表

mylist2 = [Inventory ()]
# access the object by this code below
mylist2[0].info()

或者在你完成更新 folyo 之后

mylist2.append(folyo)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM