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']
>>> map(lambda x: 'a' + str(x), source) ['a1', 'a2', 'a3']
Comments
Post a Comment