Equivalent of R's paste command for vector of numbers in Python

前端 未结 5 951
栀梦
栀梦 2020-12-16 14:52

This must have been asked before, but I\'m afraid I can\'t find the answer.

In R, I can write

paste0(\'s\', 1:10)

which returns a l

5条回答
  •  悲哀的现实
    2020-12-16 15:10

    >>> ["s" + str(i) for i in xrange(1,11)]
    ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10']
    

    EDIT: range works in both Python 2 and Python 3, but in Python 2 xrange is a little more efficient potentially (it's a generator not a list). Thansk @ytu

提交回复
热议问题