python - 如何将字典中的数值缩放到 0 到 1 的范围

标签 python dictionary

我有一本看起来像这样的字典

{u'Albania': 0.62,
 u'Algeria': 6.79,
 u'Angola': 54.31,
 u'Argentina': 0.92,
 u'Armenia': 1.28,
 u'Bangladesh': 29.0,
 u'Belarus': 0.1,
 u'Belize': 12.21,
 u'Benin': 47.33,
 u'Bhutan': 10.22,
 u'Bolivia': 15.61,
 u'Bosnia and Herzegovina': 0.04,
 u'Botswana': 31.23,
 u'Brazil': 6.14,
 u'Bulgaria': 0.0,
 u'Burkina Faso': 44.6,
 u'Burundi': 81.32,
 u'Cambodia': 22.75,
 u'Cameroon': 9.56,
 u'Cape Verde': 21.02,
 u'Central African Republic': 62.83,
 u'Chad': 61.94,
 u'Chile': 1.35,
 u'China': 11.8,
 u'Colombia': 8.16,
 u'Comoros': 46.11,
 u'Congo, Democratic Republic of the': 87.72,
 u'Congo, Republic of the': 54.1,
 u'Costa Rica': 3.12,
 u'Croatia': 0.06,
 u'Czech Republic': 0.13,
 u"C\xf4te d'Ivoire": 23.75,
 u'Djibouti': 18.84,
 u'Dominican Republic': 2.24,
 u'East Timor': 37.44,
 u'Ecuador': 4.61,
 u'Egypt': 1.69,
 u'El Salvador': 8.97,
 u'Estonia': 0.46,
 u'Ethiopia': 38.96,
 u'Fiji': 5.88,
 u'Gabon': 4.84,
 u'Gambia, The': 33.63,
 u'Georgia': 15.27,
 u'Ghana': 28.59,
 u'Guatemala': 13.53,
 u'Guinea': 43.34,
 u'Guinea-Bissau': 48.9,
 u'Guyana': 8.7,
 u'Haiti': 61.71,
 u'Honduras': 17.92,
 u'Hungary': 0.07,
 u'India': 32.67,
 u'Indonesia': 18.06,
 u'Iran': 1.45,
 u'Iraq': 2.82,
 u'Jamaica': 0.21,
 u'Jordan': 0.12,
 u'Kazakhstan': 0.11,
 u'Kenya': 43.37,
 u'Kyrgyzstan': 6.23,
 u'Laos': 33.88,
 u'Latvia': 0.14,
 u'Lesotho': 43.41,
 u'Liberia': 83.76,
 u'Lithuania': 0.16,
 u'Macedonia, Republic of': 0.0,
 u'Madagascar': 81.29,
 u'Malawi': 73.86,
 u'Malaysia': 0.0,
 u'Mali': 50.43,
 u'Mauritania': 23.43,
 u'Mexico': 1.15,
 u'Micronesia, Federated States of': 31.15,
 u'Moldova': 0.39,
 u'Montenegro': 0.12,
 u'Morocco': 2.52,
 u'Mozambique': 59.58,
 u'Namibia': 31.91,
 u'Nepal': 24.82,
 u'Nicaragua': 11.91,
 u'Niger': 43.62,
 u'Nigeria': 67.98,
 u'Pakistan': 21.04,
 u'Panama': 6.56,
 u'Papua New Guinea': 35.79,
 u'Paraguay': 7.16,
 u'Peru': 4.91,
 u'Philippines': 10.42,
 u'Poland': 0.05,
 u'Romania': 0.41,
 u'Russia': 0.0,
 u'Rwanda': 63.17,
 u'Saint Lucia': 20.93,
 u'Senegal': 33.5,
 u'Serbia': 0.25,
 u'Seychelles': 0.25,
 u'Sierra Leone': 53.37,
 u'Slovakia': 0.21,
 u'Slovenia': 0.06,
 u'South Africa': 13.77,
 u'Sri Lanka': 7.04,
 u'Sudan': 19.8,
 u'Suriname': 15.54,
 u'Swaziland': 40.63,
 u'Syria': 1.71,
 u'S\xe3o Tom\xe9 and Pr\xedncipe': 28.18,
 u'Tajikistan': 6.56,
 u'Tanzania': 67.87,
 u'Thailand': 0.37,
 u'Togo': 38.68,
 u'Trinidad and Tobago': 4.16,
 u'Tunisia': 1.35,
 u'Turkey': 0.0,
 u'Turkmenistan': 24.82,
 u'Uganda': 38.01,
 u'Ukraine': 0.06,
 u'Uruguay': 0.2,
 u'Venezuela': 6.63,
 u'Vietnam': 16.85,
 u'West Bank and Gaza': 0.04,
 u'Yemen': 17.53,
 u'Zambia': 68.51}

这些数字代表每天生活在 1.25 以下的贫困百分比。

我想在 map 上显示我正在填充从红色到绿色的不同颜色(红色是最小的数字,绿色是最高的。所以我想使用这些数字作为颜色的链接

如何减少这些数字之间的相对关系,使它们全部出现在 0-1 的范围内。其中 0 和 1 是我乘以 255 得到红色或蓝色的数字。

最佳答案

您可以使用 dict 的 values() 方法来收集所有值,然后使用 minmax 内置 python 方法来标准化您的字典。例如

d = { u'Albania': 0.62,
 u'Algeria': 6.79,
 #...
  u'Zambia': 68.51
 }

values = d.values()
min_ = min(values)
max_ = max(values)

normalized_d = {key: ((v - min_ ) / (max_ - min_) )  for (key, v) in d.iteritems() }

关于python - 如何将字典中的数值缩放到 0 到 1 的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23324068/

相关文章:

python - 为什么'new_file + = line + string'比'new_file = new_file + line + string'快得多?

Python 在子列表中查找列表长度

python - pandas:如何将 bin 值追加回原始数据框

javascript - 从 json 创建数组,但值有异常(exception)

python - 将元组列表转换为字典

python - n维数组中唯一值的索引

python - 收到错误 : 'Int64Index' object has no attribute 'get_values' . 我做错了什么?

给初学者的 Python 建议。正则表达式、字典等?

c++ - 如何为 C++ 映射创建自己的字符串比较对象

python - 如何从字符串中删除字典中对应的单词?