<p>如果您的目标是<a href="https://stackoverflow.com/questions/53731864/a-list-of-dictionaries-want-to-get-each-value-and-put-them-into-a-separate-list#comment94317749_53731864">calculate Pearson's correlation</a>,那么应该使用<code>pandas</code>。你知道吗</p>
<p>假设字典的原始列表存储在一个名为<code>output</code>的变量中。您可以使用以下方法轻松地将其转换为<code>pandas</code>数据帧:</p>
<pre><code>import pandas as pd
df = pd.DataFrame(output)
print(df)
# Total Barren Land Total Developed Total Forest Total Population: Total Water Ice Cover
#0 0.224399 17.205368 34.406421 4585 2.848142
#1 0.115141 37.271157 19.113414 4751 1.047784
#2 0.259720 23.504698 20.418608 3214 0.091666
#3 0.000000 66.375457 10.686713 5005 1.047784
</code></pre>
<p>现在您可以轻松生成相关矩阵:</p>
<pre><code># this is just to make the output print nicer
pd.set_option("precision",4) # only show 4 digits
# remove 'Total ' from column names to make printing smaller
df.rename(columns=lambda x: x.replace("Total ", ""), inplace=True)
corr = df.corr(method="pearson")
print(corr)
# Barren Land Developed Forest Population: Water Ice Cover
#Barren Land 1.0000 -0.9579 0.7361 -0.7772 0.4001
#Developed -0.9579 1.0000 -0.8693 0.5736 -0.6194
#Forest 0.7361 -0.8693 1.0000 -0.1575 0.9114
#Population: -0.7772 0.5736 -0.1575 1.0000 0.2612
#Water Ice Cover 0.4001 -0.6194 0.9114 0.2612 1.0000
</code></pre>
<p>现在您可以通过键访问各个相关性:</p>
<pre><code>print(corr.loc["Forest", "Water Ice Cover"])
#0.91135717479534217
</code></pre>