Domanda

I need to map country list in to floating number list.

country_list = ['China','India','Japan',...etc]

Mapping should be like following. (Just an example).

China  0.1
India  0.2
Japan  0.3
....   ...
....   1.0
....   1.1
....   ...
....   2.0

What is the most quick way doing this with Python.

Related Questions : Python Map List of Strings to Integer List

È stato utile?

Soluzione

Generate the floats, zip the two lists.

>>> country_list = ['China', 'India', 'Japan']
>>> numbers = list(x/10.0 for x in range(1, len(country_list)+1))
>>> zip(country_list, numbers)
[('China', 0.1), ('India', 0.2), ('Japan', 0.3)]

>>> print "\n".join("{} {}".format(x, y) for x, y in _)
China 0.1
India 0.2
Japan 0.3

EDIT: replaced the float(x)*0.1 to a division.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top