如何将函数应用于变量输入列表?例如,filter函数返回true值,但不返回函数的实际输出.
from string import upper
mylis=['this is test', 'another test']
filter(upper, mylis)
['this is test', 'another test']
Run Code Online (Sandbox Code Playgroud)
预期的产出是:
['THIS IS TEST', 'ANOTHER TEST']
Run Code Online (Sandbox Code Playgroud)
我知道upper是内置的.这只是一个例子.
我有一个这样的列表:
a = ((4.0, 4, 4.0), (3.0, 3, 3.6), (3.5, 6, 4.8))
Run Code Online (Sandbox Code Playgroud)
我想要一个像这样的结果(列表中的每个第一个元素):
4.0, 3.0, 3.5
Run Code Online (Sandbox Code Playgroud)
我尝试了[:: 1] [0],但它不起作用
我刚刚开始学习Python几周前.Python版本= 2.7.9
在 Python 中,我曾经通过以下方式获取二维列表的第一个元素
a = [[0, 1], [2, 3]]
a[:][0]
# [0, 2]
Run Code Online (Sandbox Code Playgroud)
现在,列表有点复杂,获取第一个元素的方法不起作用
a = [['sad', 1], ['dsads', 2]]
a[:][0]
# ['sad', 1]
Run Code Online (Sandbox Code Playgroud)
我不知道这里有什么区别。以及如何以这种简单的方式获取第一个元素,而不是
[e[0] for e in a]
Run Code Online (Sandbox Code Playgroud)