**kwds和self.__dict__.update()函数联合批量更新字典

import pandas as pd

d1 = [[3,"negative",2],[4,"negative",6],[11,"positive",0],[12,"positive",2],
[14,"positive",2]]
df1 = pd.DataFrame(d1, columns=["xuhao","result","value"])
print(df1,"\n")

class Updatadict:
        def __init__(self, **kwds): 
            self.__dict__.update(kwds) #更新字典
            print(self.__dict__)


# Create Updatadict
mydata = Updatadict(
    data=df1[0:2],
    list1=[3,"negative",2],
    name="lele",
    info={"age":1,"color":"grey"}
    )

print(mydata.data)
print(mydata.list1)

输出结果

xuhao    result  value
0      3  negative      2
1      4  negative      6
2     11  positive      0
3     12  positive      2
4     14  positive      2

{'data':    xuhao    result  value
0      3  negative      2
1      4  negative      6, 'list1': [3, 'negative', 2], 'name': 'lele', 'info': {'age': 1, 'color': 'grey'}}
#print(mydata.data)
   xuhao    result  value
0      3  negative      2
1      4  negative      6
#print(mydata.list1)
[3, 'negative', 2]

 

posted @ 2023-03-16 22:08  yayagogogo  阅读(32)  评论(0编辑  收藏  举报