【发布时间】:2015-03-24 05:28:45
【问题描述】:
我有一个 for 循环:
for x in range(1,13):
print ("This was the average temperature in month number " + str(x) + " in Boston, 2014: ", Boston_monthly_temp(x))
这会打印出 2014 年波士顿的月平均气温,例如:
This was the average temperature in month number 1 in Boston, 2014: 26.787096774193547
一直到第 12 个月(十二月):
This was the average temperature in month number 12 in Boston, 2014: 38.42580645161291.
总而言之,这个 for 循环产生 12 行。
但是,我不知道如何将这个“for”循环的结果存储到单个变量中,例如 (output_number_one)。
我正在尝试将结果存储到单个变量中,因此我可以将变量(及其内容)转储/写入到 pickle 文件中,称为:
output.pkl
【问题讨论】:
-
由于“保存到变量”,您可以放弃
print(因为它用于副作用而不是值生成)在这种情况下,List Comprehension 将是一种惯用方法(该链接还显示了如何显式构建列表)。
标签: python persistence pickle