Python:功能map()列表指定字段值的列表

时间:2016-10-01 20:20:44

标签: python-2.7 lambda functional-programming

您好了下面的java 8代码,我如何在python 2.7中执行以下操作

Array(6);     // Creates an array containing six empty slots
Array(null);  // Creates an array containing one null
Array(6,6,6); // Creates an array containing three 6
[6];          // Creates an array containing one 6
[null];       // Creates an array containing one null
[6,6,6];      // Creates an array containing three 6

请记住,因为我在python中迭代字典,所以modulename作为模块['modulename']

进行访问

1 个答案:

答案 0 :(得分:1)

据我所知,这是Python中的简单list comprehension

[module['modulename'] for module in modules]

或者,map()itemgetter()

In [1]: modules = [{'module': 'module1'}, {'module': 'module2'}]

In [2]: from operator import itemgetter

In [3]: map(itemgetter('module'), modules)  # on Python3, you would need to call list() on it to see the same result
Out[3]: ['module1', 'module2']