每次代码执行后持续增加 python 列表大小
Persistent increase of a python list size after every code execution
我的代码以空列表开头:
l = []
假设我想在每次 运行 我的代码时将 5 个元素附加到我的列表中:
l += [0, 0, 0, 0, 0]
print(l) . # reuslt is l = [0, 0, 0, 0, 0]
代码执行后,此信息丢失。
我想知道每次我再次 运行 我的代码时,我的列表如何保持增长五个零。
first run >>> [0, 0, 0, 0, 0]
second run >>> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
third run >>> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
.
.
.
根据定义,您的所有程序变量都是程序范围的局部变量。当您退出该程序时,操作系统将回收 space。要使数据在程序结束后持久存在,您需要将其存储在其他地方,例如文件中——一种在 Python 运行 时间系统的易变存在之外的资源。
我相信你能查到文件操作。
执行后您将丢失内存中的所有数据。因此,在进程结束后,将无法将您的列表保存在 ram 中。您必须将列表写入文件并在每次执行代码时读取该文件。
这可能会有所帮助:
http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
您需要在 运行 秒之间保留数据。一种方法是使用 pickle
模块,我将在这里演示它,因为它非常简单。另一种方法是使用 JSON
。这些方法可以保存(或序列化)一个 Python 数据对象。这不同于仅将文本写入文本文件。
import pickle
my_list = [0, 0, 0, 0, 0]
# Save my_list
file = open("save.txt", "wb") # "wb" means write binary (as opposed to plain text)
pickle.dump(my_list, file)
file.close()
# Close and restart your Python session
file = open("save.txt", "rb") # "rb" means read binary
new_list = pickle.load(file)
file.close()
print(new_list) # -> [0, 0, 0, 0, 0]
编辑:您声明希望每次代码为 运行 时自动添加列表。您可以通过加载后追加然后再次保存来实现。
import pickle
# Create an empty list
my_list = []
# Try to load the existing list in a try block in case the file does not exist:
try:
file = open("save.txt", "rb") # "rb" means read binary
loaded_list = pickle.load(file)
file.close()
my_list += loaded_list
except (OSError, IOError):
print("File does not exist")
# Append to the list as you want
my_list += [0, 0, 0, 0, 0]
# Save the list again
file = open("save.txt", "wb")
pickle.dump(my_list, file)
file.close()
print(my_list) # This will get bigger every time the script in run
我的代码以空列表开头:
l = []
假设我想在每次 运行 我的代码时将 5 个元素附加到我的列表中:
l += [0, 0, 0, 0, 0]
print(l) . # reuslt is l = [0, 0, 0, 0, 0]
代码执行后,此信息丢失。 我想知道每次我再次 运行 我的代码时,我的列表如何保持增长五个零。
first run >>> [0, 0, 0, 0, 0]
second run >>> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
third run >>> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
.
.
.
根据定义,您的所有程序变量都是程序范围的局部变量。当您退出该程序时,操作系统将回收 space。要使数据在程序结束后持久存在,您需要将其存储在其他地方,例如文件中——一种在 Python 运行 时间系统的易变存在之外的资源。
我相信你能查到文件操作。
执行后您将丢失内存中的所有数据。因此,在进程结束后,将无法将您的列表保存在 ram 中。您必须将列表写入文件并在每次执行代码时读取该文件。
这可能会有所帮助: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
您需要在 运行 秒之间保留数据。一种方法是使用 pickle
模块,我将在这里演示它,因为它非常简单。另一种方法是使用 JSON
。这些方法可以保存(或序列化)一个 Python 数据对象。这不同于仅将文本写入文本文件。
import pickle
my_list = [0, 0, 0, 0, 0]
# Save my_list
file = open("save.txt", "wb") # "wb" means write binary (as opposed to plain text)
pickle.dump(my_list, file)
file.close()
# Close and restart your Python session
file = open("save.txt", "rb") # "rb" means read binary
new_list = pickle.load(file)
file.close()
print(new_list) # -> [0, 0, 0, 0, 0]
编辑:您声明希望每次代码为 运行 时自动添加列表。您可以通过加载后追加然后再次保存来实现。
import pickle
# Create an empty list
my_list = []
# Try to load the existing list in a try block in case the file does not exist:
try:
file = open("save.txt", "rb") # "rb" means read binary
loaded_list = pickle.load(file)
file.close()
my_list += loaded_list
except (OSError, IOError):
print("File does not exist")
# Append to the list as you want
my_list += [0, 0, 0, 0, 0]
# Save the list again
file = open("save.txt", "wb")
pickle.dump(my_list, file)
file.close()
print(my_list) # This will get bigger every time the script in run