如何在python中将嵌套循环转换为列表理解 [英] How to convert a nested loop to a list comprehension in python

查看:62
本文介绍了如何在python中将嵌套循环转换为列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将这段代码转换为python中的列表理解

How can i convert this code to list comprehension in python

max_len = []
for word in fourier_dict:
    word = fourier_dict[word]
    for occur in word:
        max_len.append(len(occur))

我是python的新手,我必须将此嵌套循环转换为我无法弄清楚的列表理解.一点帮助将不胜感激.

Im new to python and i have to convert this nested loop to a list comprehension which i cant figure out. A little help would be really appreciated.

推荐答案

max_len = [len(occur) for word in fourier_dict for occur in fourier_dict[word]]

应该工作.

这篇关于如何在python中将嵌套循环转换为列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆