开发者

How to append a list to a list created by a function in Python

Okay, I have a list that looks like this

OldList = [1000, 2000, 3000, 4000, 5000]

And I want to run all开发者_如何学Python members of that list through a function called ListMultiply, like so

NewList = ListMultiply("/listfile/" + oldList]

How do I do this without concatenating string/list? Thanks.


NewList = [ListMultiply("/listfile/"+str(e)) for e in OldList]

The above will create a new list by adding the string "/listfile/" to the string representation of each element and passing the result to ListMultiply().


You should concatenate the string/list somewhere("".join or str.format would be better anyway), but I think you look something like:

>>> OldList = [1000, 2000, 3000, 4000, 5000]
>>> def f(x):
...     return x*2
... 
>>> OldList = [1000, 2000, 3000, 4000, 5000]
>>> NewList = [f("listfile/" + str(i)) for i in OldList]
>>> NewList
['listfile/1000listfile/1000', 'listfile/2000listfile/2000', 'listfile/3000listfile/3000', 'listfile/4000listfile/4000', 'listfile/5000listfile/5000']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜