简体   繁体   English

Python列表列表->使元素n成为子列表

[英]Python list of lists -> make sublists of element n

If I have a list of lists such as: 如果我有清单清单,例如:

[[2, 0.84], [2, 0.75], [2, 0.63], [2, 0.6]]

Is there a quick way to get a list of all of the 2nd elements of the inner list? 有没有一种快速的方法来获取内部列表的所有第二个元素的列表? Ie, the example would return: 即,该示例将返回:

[0.84, 0.75, 0.63, 0.6]

I'm not entirely sure why this doesn't work: 我不确定这为什么行不通:

list[:][0]

But that only return a pair of values in a list. 但这只会在列表中返回一对值。

try: 尝试:

>>> my_list = [[2, 0.84], [2, 0.75], [2, 0.63], [2, 0.6]]
>>> [item[1] for item in my_list]
[0.84, 0.75, 0.63, 0.6]

Your try list[:][0] does not work, because list[:] makes a copy of list and list[:][0] takes the first sublist of the copy, not first elements of sublists. 您的尝试list[:][0]不起作用,因为list[:]会复制listlist[:][0]将获取副本的第一个子列表,而不是子列表的第一个元素。 Also, avoid naming your variables list because it shadows the built-in list . 另外,避免命名变量list因为它会遮盖内置list

Here is a solution that gives you the desired output, but as shown in the answer by @Slater Tyranus, it is not the most efficient approach. 这是一个为您提供所需输出的解决方案,但是正如@Slater Tyranus的回答所示,这不是最有效的方法。

In [34]: a = [[2, 0.84], [2, 0.75], [2, 0.63], [2, 0.6]]

In [36]: list(zip(*a)[1])
Out[36]: [0.84, 0.75, 0.63, 0.6]

Hasan is right in the general case, but if you're typically looking for this kind of slicing I would suggest you start using numpy, which is really made for this kind of thing. Hasan在一般情况下是正确的,但是如果您通常在寻找这种切片,我建议您开始使用numpy,这实际上是为这种事情而做的。 Specifically: 特别:

>>> import numpy as np

>>> my_array = np.array([[2, 0.84], [2, 0.75], [2, 0.63], [2, 0.6]])
>>> my_array[:, 1]
[0.84, 0.75, 0.63, 0.6]

Personally, I find numpy slicing to be much easier to work with, and it seems to align very well with your intuition about the problem above. 就我个人而言,我发现numpy切片更容易使用,并且它似乎与您对上述问题的直觉非常吻合。 Also, it's liable to be MUCH faster with large arrays. 而且,使用大型阵列时,速度可能会更快。 That said, apparently much slower for arrays at this size, posting timeit results below: 也就是说,对于这种大小的阵列,显然要慢得多,在下面发布时间结果:

>>> my_list = [[2, 0.84], [2, 0.75], [2, 0.63], [2, 0.6]]
>>> my_array = np.array(my_list)

>>> timeit.timeit("my_array[:, 1]", setup="from __main__ import my_array", number=10000)
0.016569137573242188

>>> timeit.timeit("[item[1] for item in my_list]", setup="from __main__ import my_list", number=10000)
0.006146907806396484

>>> timeit.timeit("list(zip(*my_list)[1])", setup="from __main__ import my_list", number=10000)
0.013128042221069336

Just to show a slightly clearer picture, I ran the results on a larger example to show how these different approaches scale: 为了显示更清晰的图片,我在一个较大的示例中运行了结果,以显示这些不同方法的缩放比例:

>>> my_array = np.random.rand(100,100)
>>> my_list = my_array.tolist()

>>> timeit.timeit("my_array[:, 1]", setup="from __main__ import my_array", number=10000)
0.019372940063476562

>>> timeit.timeit("[item[1] for item in my_list]", setup="from __main__ import my_list", number=10000)
0.07012009620666504

>>> timeit.timeit("list(zip(*my_list)[1])", setup="from __main__ import my_list", number=10000)
1.2308871746063232

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM