Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从for循环将数字放入列表中_Python_List_For Loop - Fatal编程技术网

Python 如何从for循环将数字放入列表中

Python 如何从for循环将数字放入列表中,python,list,for-loop,Python,List,For Loop,我目前正在测试一个库,它是matplotlib.pyplot,我只想将我在for循环中计算的一系列数字绘制成图形。如何将这些数字放入列表中,以便在Y轴和X轴上绘制相应的数字 这是我的密码 import matplotlib.pyplot as plt file = open("Estimate Year.txt", "w") current_pop = 7650000000 for i in range(2020, 2100 + 1): yea

我目前正在测试一个库,它是
matplotlib.pyplot
,我只想将我在for循环中计算的一系列数字绘制成图形。如何将这些数字放入列表中,以便在Y轴和X轴上绘制相应的数字

这是我的密码

import matplotlib.pyplot as plt

file = open("Estimate Year.txt", "w")
current_pop = 7650000000

for i in range(2020, 2100 + 1):
    year = str(i)
    current_pop += int(current_pop * 0.011)
    file.write("------------------------------------------------\n| Population: "
               + str(current_pop) + "     |    Year: "
               + year + "  |\n")

我知道如何使用这个库,但我正在努力将每个迭代的人口放入一个列表中。。。例如[7,10,13,15,19…,]等等,我从2020年开始到2100年结束的这几年也是一样的,一种方法是使用专门为科学计算而设计的
numpy
。在这里,基本上有等距的年份,然后是一个幂函数,取决于初始年份和当前年份之间的距离。以下内容似乎是您想要完成的:

>>> import numpy as np
>>> arr = np.linspace(2020, 2101, 82)
>>> arr
array([2020., 2021., 2022., 2023., 2024., 2025., 2026., 2027., 2028.,
       2029., 2030., 2031., 2032., 2033., 2034., 2035., 2036., 2037.,
       2038., 2039., 2040., 2041., 2042., 2043., 2044., 2045., 2046.,
       2047., 2048., 2049., 2050., 2051., 2052., 2053., 2054., 2055.,
       2056., 2057., 2058., 2059., 2060., 2061., 2062., 2063., 2064.,
       2065., 2066., 2067., 2068., 2069., 2070., 2071., 2072., 2073.,
       2074., 2075., 2076., 2077., 2078., 2079., 2080., 2081., 2082.,
       2083., 2084., 2085., 2086., 2087., 2088., 2089., 2090., 2091.,
       2092., 2093., 2094., 2095., 2096., 2097., 2098., 2099., 2100.,
       2101.])
>>> current_pop = 7650000000 * pow(1.011, arr - 2020)
>>> current_pop
array([7.65000000e+09, 7.73415000e+09, 7.81922565e+09, 7.90523713e+09,
       7.99219474e+09, 8.08010888e+09, 8.16899008e+09, 8.25884897e+09,
       8.34969631e+09, 8.44154297e+09, 8.53439994e+09, 8.62827834e+09,
       8.72318940e+09, 8.81914449e+09, 8.91615508e+09, 9.01423278e+09,
       9.11338934e+09, 9.21363663e+09, 9.31498663e+09, 9.41745148e+09,
       9.52104345e+09, 9.62577493e+09, 9.73165845e+09, 9.83870669e+09,
       9.94693247e+09, 1.00563487e+10, 1.01669686e+10, 1.02788052e+10,
       1.03918721e+10, 1.05061827e+10, 1.06217507e+10, 1.07385899e+10,
       1.08567144e+10, 1.09761383e+10, 1.10968758e+10, 1.12189414e+10,
       1.13423498e+10, 1.14671156e+10, 1.15932539e+10, 1.17207797e+10,
       1.18497083e+10, 1.19800551e+10, 1.21118357e+10, 1.22450659e+10,
       1.23797616e+10, 1.25159390e+10, 1.26536143e+10, 1.27928041e+10,
       1.29335249e+10, 1.30757937e+10, 1.32196274e+10, 1.33650433e+10,
       1.35120588e+10, 1.36606914e+10, 1.38109590e+10, 1.39628796e+10,
       1.41164713e+10, 1.42717524e+10, 1.44287417e+10, 1.45874579e+10,
       1.47479199e+10, 1.49101470e+10, 1.50741587e+10, 1.52399744e+10,
       1.54076141e+10, 1.55770979e+10, 1.57484459e+10, 1.59216789e+10,
       1.60968173e+10, 1.62738823e+10, 1.64528950e+10, 1.66338769e+10,
       1.68168495e+10, 1.70018349e+10, 1.71888550e+10, 1.73779324e+10,
       1.75690897e+10, 1.77623497e+10, 1.79577355e+10, 1.81552706e+10,
       1.83549786e+10, 1.85568834e+10])
如果您特别需要列表,可以使用
tolist()
方法将numpy数组转换为列表

import matplotlib.pyplot as plt

file = open("Estimate Year.txt", "w")
current_pop = 7650000000
population = []
year_graph = []
for i in range(2020, 2100 + 1):
    year = str(i)
    year_graph.append(i)
    current_pop += int(current_pop * 0.011)
    population.append(current_pop)
    file.write("------------------------------------------------\n| Population: "
               + str(current_pop) + "     |    Year: "
               + year + "  |\n")

plt.xlabel("Year")
plt.ylabel("Population")
plt.plot(year_graph, population)
plt.show()
以下是最终输出:

hi,也许创建一个列表并在循环中附加到它-
mylist=list();list.append(1)
@IronMan我试过了,列表正在为迭代过程中计算的数字中的每个数字创建索引。当它应该是[18556883298,…]