I have a dictionary of country names, for each country I have stored a numerical value and a tag. the numerical value is a simple frequency I pass to the pygal Worldmap and the tag is a string I want to add to the tooltip.
For example suppose country 'af' and 'cn' have corresponding numerical values 20 and 10, and tags 'AA' and 'BB'. Then the below code works just fine.
import pygal
worldmap_chart = pygal.maps.world.World()
worldmap_chart.add('2012', [{'value': ('af',20),
'formatter': lambda x: '{}: {}'.format(x[0], x[1]) + 'AA'},
{'value': ('cn',10),
'formatter': lambda x: '{}: {}'.format(x[0], x[1]) + 'BB'}])
But if I create a list like this
countries=[{'value': ('af',20), 'tag':'AA'}, {'value': ('cn',10), 'tag':'BB'}]
And pass it to the above function like this
worldmap_chart.add('2012', [{'value': i['value'],
'formatter': lambda x: '{}: {}'.format(x[0], x[1]) + i['tag']} for i in countries])
It doesn't give me the same thing! both of the tooltips now show the same tag BB
corresponding to the last element in the countries
.
In fact I have tens of countries and with the last code, the tooltips are all the same and they are the tag for the last country. but the first code at the top works fine and I wonder how can I generate them correctly.
The variable
i
is changing. You do not callformatter
function until later, at which pointi
already points to the last element of the for loop. If you could call the formatter straight after it finishes the first element and before it continues to the second, you would getAA
. What you need to do is bindi['tag']
to a parameter of the lambda -tag=i["tag"]
so that the value itself will be saved.What it prints is: