扫码关注官方订阅号
小伙看你根骨奇佳,潜力无限,来学PHP伐。
years=['2016','2014','2013','2009','2005'] msg = ['This year is %s' % k for k in years] print msg
这个正好算是Python语言精练的一个特点了:可以很容易根据已有的“容器”构建新的“容器”。容器指的是list/tuple/dict/iter 之类的实现了__iter__方法的类型。
__iter__
比如想要构建字典:
print {k: int(k) for k in years} # 该写法在Python2.7以后支持 # 输出: {'2014': 2014, '2016': 2016, '2005': 2005, '2013': 2013, '2009': 2009}
years = ['2016', '2014', '2013', '2009', '2005'] msg = map(lambda a: "This year is {0}".format(a), years) print(msg)
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
这个正好算是Python语言精练的一个特点了:可以很容易根据已有的“容器”构建新的“容器”。容器指的是list/tuple/dict/iter 之类的实现了
__iter__方法的类型。比如想要构建字典: