我正在使用Python 2.6,并希望通过'keyN'生成'key1'的最有效方法?我曾尝试没有成功:什么是生成包含'key1'到'keyN'的列表的最有效方法?
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> N=10
>>> x = range(1, N + 1)
>>> 'key' + str(x)
'key[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'
>>> map('key' + _, map(str, range(N + 1)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> map('key' + ?, map(str, range(N + 1)))
File "<stdin>", line 1
map('key' + ?, map(str, range(N + 1)))
^
SyntaxError: invalid syntax
>>> map('key' + *, map(str, range(N + 1)))
File "<stdin>", line 1
map('key' + *, map(str, range(N + 1)))
^
SyntaxError: invalid syntax
>>>
我认为这个问题不值得赞赏。这是一个有效的问题,它包括一系列已经尝试过的方法。由于他们不工作,OP正在寻求解决方案。为什么downvote呢? –