社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  JohnGalt  »  全部回复
回复总数  1
6 年前
回复了 JohnGalt 创建的主题 » 在依赖于前面元素的python中创建列表

所以,我猜如果他们要求你在练习中使用列表理解,他们希望你直接定义列表:

List comprehensions Docs

尽管你的解决方案可能有效,但它们可能不是问题的制定者想要的。

def build_list(number_of_randoms=3, outer_list_size=3, threshold=5):

rands = [[random.randint(1, 10) for _x in range(number_of_randoms)] for _y in range(outer_list_size)]
return [[index] + [n for n in numbers] + [sum(x for x in numbers if x > threshold)] for index, numbers in enumerate(rands)]

我在上面写了一个相当粗糙的函数,但它应该能帮助您了解我认为他们在寻找什么。