Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
获取二维列表。我想用每个列表中的第 i 个元素创建一个新列表。做这个的最好方式是什么?
我有:
map(lambda x: x[i], l)
这是一个例子
>>> i = 0 >>> l = [[1,10],[2,20],[3,30]] >>> map(lambda x: x[i], l) [1, 2, 3]
使用列表理解:
i = 1 data = [[1,10],[2,20],[3,30]] result = [d[i] for d in data] # [10, 20, 30]
另请参阅有关列表理解与地图的此问题。