-1

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

9
  • 2
    Wait, what? You suspect that there is a duplicate to your question, and you post it anyway? Why? :| Commented Mar 27, 2014 at 11:20
  • Mapping to what? where are those numebrs? in a list with the same indexes? If that is yes, use zip, someone mentioned that in the response you linked to. Commented Mar 27, 2014 at 11:21
  • @BartoszKP: That is different from what I need, but it addresses a similar question, That is why I indicated it. Commented Mar 27, 2014 at 11:22
  • @Raul Guiu : I need to map it to floating points starting 0.1 to ..n? Commented Mar 27, 2014 at 11:23
  • 1
    @NilaniAlgiriyage All right, I understand. Perhaps use the word "related" next time then :) Commented Mar 27, 2014 at 11:26

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.