第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從對象創(chuàng)建列表

如何從對象創(chuàng)建列表

蕪湖不蕪 2023-03-22 16:28:41
我有一個清單程序,它從 excel 文件中讀取列表,然后創(chuàng)建一個類。我想覆蓋列表中對象的新值,然后將列表導出到 excel。在這里,我從 excel 文件中導入列表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()然后我創(chuàng)建類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])現(xiàn)在,我想創(chuàng)建一個新列表 mylist2,其中包含以前的庫存量和 folyo 的新值。mylist2=[]folyo.info()folyo.add(999999)folyo.update()folyo.info()print(mylist2)在此代碼中,輸出如下:Product code: 0 Amount: 500 Unit: m2 Place: storage_1Product 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
查看完整描述

2 回答

?
一只萌萌小番薯

TA貢獻1795條經(jīng)驗 獲得超7個贊

解決方法如下:


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


查看完整回答
反對 回復 2023-03-22
?
暮色呼如

TA貢獻1853條經(jīng)驗 獲得超9個贊

解決方法如下:


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


查看完整回答
反對 回復 2023-03-22
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號