Okay, I have a list that looks like this
OldList = [1000, 2000, 3000, 4000, 5000]
And I want to run all 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]
上面的代码通过将字符串"/listfile/"
到每个元素的字符串表示形式并将结果传递给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']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.