python使用generator生成list

本文介绍了Python中如何使用generator生成list,generator作为iterator的一种,可以便捷地构建mutable sequences。内容涉及列表的不同构造方式,包括空列表[]、通过逗号分隔的元素[a, b, c],列表推导式[x for x in iterable],以及type构造函数list()或list(iterable)。" 80830449,7809227,SpringBoot内部配置数据源切换切面,"['SpringBoot', 'AOP', '数据源管理']
摘要由CSDN通过智能技术生成

list官方文档:

  • Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).

  • class list([iterable])

    • Lists may be constructed in several ways:
      Using a pair of square brackets to denote the empty list: []
      Using square brackets, separating items with commas: [a], [a, b, c]
      Using a list comprehension: [x for x in iterable]
      Using the type constructor: list() or list(iterable)

generator本身就是一个iterator,所以肯定是iterable的。

eg:

def take (n):
    for i in range(n):
        yield i

l = list(take(4))

>>> l = [0,1,2,3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值