简体   繁体   English

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

[英]How to scale numerical values in a dict to range from 0 to 1

I have a dictionary that looks like this 我有一本这样的字典

{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}

these numbers represent poverty percents living under 1.25 per day. 这些数字代表每天生活在1.25以下的贫困百分比。

I want to display on a map that I am floodfilling different colors from red to green (red being smallest number and green being highest. So I want to use the numbers as a link to color 我想在地图上显示我要填充从红色到绿色的不同颜色(红色是最小的数字,绿色是最大的。所以我想使用数字作为颜色的链接

How can I reduce these numbers relatively to each other so that all of them occur with the range of 0-1. 我如何才能相对减少这些数字,以便所有这些数字都出现在0-1的范围内。 Where 0 and 1 will be the number I multiply by 255 to get red or blue. 其中0和1将是我乘以255得出红色或蓝色的数字。

You can use values() method of dict to gather all values, then min and max builtin python method to normalise your dictionary. 您可以使用dict的values()方法来收集所有值,然后使用minmax内置的python方法来规范化字典。 For instance 例如

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() }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM