在 Python 的项目级别扩展嵌套的 class 列表

Extending nested class lists on item level in Python

我想设置一个包含 'multiple dataframes'

的更新泡菜文件

因此我有一个列表 db_container,它由其他列表("the dataframe-category":alert1, alert2, alert3)组成,其中包括实际数据帧(例如 alert1 有 n 个不同的数据帧但它们总是具有相同的列名)

现在,我在使用来自 db_container

的新数据扩展我的初始 db 时遇到了问题

我的问题是在数据帧级别扩展 db

在我 运行 之后,我的代码 db 有 6 个项目而不是最初的 3 个(alert1、alert2、alert3)。

dbdb_container 应该总是开头的 3 "dataframe categories".

有什么建议吗?

def pickle_me():

    # Bind Lists of DataFrames into one Object
    db_container = [alert1, alert2, alert3]

    # if a db/pickle already exists then open old one and append with new input
    if os.path.exists(base_path+pickle_db):
       with open(base_path+pickle_db,'rb') as rfp:
          db = pickle.load(rfp)

          db.append(db_container) #-> After this code section db has 6 lists


    pickle.dump(db, open(base_path + pickle_db, 'wb'))

# If no db exists then create one.
else:
    pickle.dump(db_container, open(base_path + pickle_db, 'wb'))

谢谢。

我 运行 你的代码 db_container = [1, 2, 3] 并且在第二次通过时,观察到 db[1, 2, 3, [1, 2, 3]] 正如我预期的那样。该列表没有 6 个项目:它有 4 个项目,最后一个是包含三个项目的列表。如果您打算将列表添加到一起,您应该使用 db += db_container 而不是使用 append,后者需要一个项目。