python竖着输出_Python str.format()---传入列表中,输出列表

您无法使用单一格式显示您的输出。您可以使用列表-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这里(如果您正在使用PY 3,紧裹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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值