您好了下面的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']
进行访问答案 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']