列表长度+字符串格式+ python

时间:2014-03-12 19:16:00

标签: python string

如何更改字符串" Point"成为变量输入(字符串)?

result = []
list =  [[a,b,c,d][a,b,c,d]]

result.append( [["Point {}".format(i) for i in range(1, len(sub)+1)] for sub in list])

我基本上不想使用字符串" Point"作为常数而不是另一个参数值。我试过这个:

result = []
list =  [[a,b,c,d][a,b,c,d]]
p_name = "Number"

result.append( [[p_name, {}.format(i) for i in range(1, len(sub)+1)] for sub in list])

我想最终:

result = [[Number 1, Number 2, Number 3, Number 4][Number 1, Number 2, Number 3, Number 4]]

谢谢,

1 个答案:

答案 0 :(得分:1)

result.append( [["{} {}".format(p_name, i) for i in range(1, len(sub)+1)] for sub in list])