Python str.format() --- 传入列表,输出列表
Python str.format() --- passing in list, outputting list
我以前没怎么用过 str.format(),我想知道如何传入列表值并检索包含传入这些值的列表输出。
即
listex = range(0, 101, 25)
baseurl = "https://thisisan{0}example.com".format(*listex)
我想要一个像 ["https://thisisan0example.com", "https://thisisan25example.com", "https://thisisan50example.com", etc etc]
这样的列表输出
但是根据我上面的当前代码,当我 运行 print baseurl
时,我只会得到 "https://thisisan0example.com"
您无法使用单一格式获得如图所示的输出。您可以使用 list-comp
>>> listex = range(0, 101, 25)
>>> ["https://thisisan{}example.com".format(i) for i in listex]
['https://thisisan0example.com', 'https://thisisan25example.com', 'https://thisisan50example.com', 'https://thisisan75example.com', 'https://thisisan100example.com']
你可以在这里使用map
(如果你使用的是Py3,包装一个list
调用),
>>> map("https://thisisan{}example.com".format,listex)
['https://thisisan0example.com', 'https://thisisan25example.com', 'https://thisisan50example.com', 'https://thisisan75example.com', 'https://thisisan100example.com']
然后可以将其作为
存储在您的变量 baseurl
中
baseurl = map("https://thisisan{}example.com".format,listex)
可以使用timeit
进行速度比较
$ python -m timeit 'listex = range(0, 101, 25);["https://thisisan{}example.com".format(i) for i in listex]'
1000000 loops, best of 3: 1.73 usec per loop
$ python -m timeit 'listex = range(0, 101, 25);map("https://thisisan{}example.com".format,listex)'
1000000 loops, best of 3: 1.36 usec per loop
如您所见,map
速度更快(Python2),因为没有涉及 lambda
您可以列出补偿:
baseurl = ["https://thisisan{0}example.com".format(x) for x in listex]
这实质上是循环遍历 listex
并为每个元素使用给定字符串的唯一版本构建 baseurl
。
方法一:使用map和lambda:
baseurl = map(lambda x: "https://thisisan{0}example.com".format(x), listex)
['https://thisisan0example.com',
'https://thisisan25example.com',
'https://thisisan50example.com',
'https://thisisan75example.com',
'https://thisisan100example.com']
此处,map()
会将 lambda
函数应用于 listex
和 return 的每个元素,其中元素已被 lambda
函数更改.
方法二:使用列表理解:
baseurl = ["https://thisisan{0}example.com".format(item) for item in listex]
['https://thisisan0example.com',
'https://thisisan25example.com',
'https://thisisan50example.com',
'https://thisisan75example.com',
'https://thisisan100example.com']
在这里,我们使用列表推导来达到预期的效果。
我以前没怎么用过 str.format(),我想知道如何传入列表值并检索包含传入这些值的列表输出。
即
listex = range(0, 101, 25)
baseurl = "https://thisisan{0}example.com".format(*listex)
我想要一个像 ["https://thisisan0example.com", "https://thisisan25example.com", "https://thisisan50example.com", etc etc]
但是根据我上面的当前代码,当我 运行 print baseurl
"https://thisisan0example.com"
您无法使用单一格式获得如图所示的输出。您可以使用 list-comp
>>> listex = range(0, 101, 25)
>>> ["https://thisisan{}example.com".format(i) for i in listex]
['https://thisisan0example.com', 'https://thisisan25example.com', 'https://thisisan50example.com', 'https://thisisan75example.com', 'https://thisisan100example.com']
你可以在这里使用map
(如果你使用的是Py3,包装一个list
调用),
>>> map("https://thisisan{}example.com".format,listex)
['https://thisisan0example.com', 'https://thisisan25example.com', 'https://thisisan50example.com', 'https://thisisan75example.com', 'https://thisisan100example.com']
然后可以将其作为
存储在您的变量baseurl
中
baseurl = map("https://thisisan{}example.com".format,listex)
可以使用timeit
$ python -m timeit 'listex = range(0, 101, 25);["https://thisisan{}example.com".format(i) for i in listex]'
1000000 loops, best of 3: 1.73 usec per loop
$ python -m timeit 'listex = range(0, 101, 25);map("https://thisisan{}example.com".format,listex)'
1000000 loops, best of 3: 1.36 usec per loop
如您所见,map
速度更快(Python2),因为没有涉及 lambda
您可以列出补偿:
baseurl = ["https://thisisan{0}example.com".format(x) for x in listex]
这实质上是循环遍历 listex
并为每个元素使用给定字符串的唯一版本构建 baseurl
。
方法一:使用map和lambda:
baseurl = map(lambda x: "https://thisisan{0}example.com".format(x), listex)
['https://thisisan0example.com',
'https://thisisan25example.com',
'https://thisisan50example.com',
'https://thisisan75example.com',
'https://thisisan100example.com']
此处,map()
会将 lambda
函数应用于 listex
和 return 的每个元素,其中元素已被 lambda
函数更改.
方法二:使用列表理解:
baseurl = ["https://thisisan{0}example.com".format(item) for item in listex]
['https://thisisan0example.com',
'https://thisisan25example.com',
'https://thisisan50example.com',
'https://thisisan75example.com',
'https://thisisan100example.com']
在这里,我们使用列表推导来达到预期的效果。