开发者

The list with most elements

I have a list with dictionary as elements. Each dictionary has an entry called type. The type field represents a list. What is the simplest/pythonic way of obtaini开发者_如何学编程ng the list with the most elements?

programmes_by_type = []
programmes_by_type.append({'type' : [1, 2, 3]})
programmes_by_type.append({'type' : [2, 5]})
programmes_by_type.append({'type' : [3]})
programmes_by_type.append({'type' : [11, 2, 6, 7]})

Given the previous example it should return the [11, 2, 6, 7] list.


max([option['type'] for option in programmes_by_type], key=len)


max_length = 0
type_list = None

for el in programmes_by_type:
    if len(el) > max_length:
        max_length = len(el)
        type_list = el['type']

type_list now contains the biggest list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜