python - converting list of numbers into list of strings appending some strings -


this question related converting list of numbers list of strings appending strings.

how convert source below :

source = list (range (1,4)) 

into result below:

result = ('a1', 'a2', 'a3') 

you can either use list comprehension:

# in python 2, range() returns `list`. so, don't need wrap in list() # in python 3, however, range() returns iterator. need wrap  # in `list()`. can choose accordingly. infer python 3 code.  >>> source = list(range(1, 4)) >>> result = ['a' + str(v) v in source] >>> result ['a1', 'a2', 'a3'] 

or map() lambda:

>>> map(lambda x: 'a' + str(x), source) ['a1', 'a2', 'a3'] 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -