[英]Accessing given element from each sublist of multidimensional list without for loop
Say I have a list of lists for example:假设我有一个列表列表,例如:
list0 = [[a, .5, .3], [b, .3, .8], [c, .7, 1.3], [d, 1.03, .2]]
I want to call the second element of each sublist for output to a list I assumed this would work.我想将 output 的每个子列表的第二个元素调用到我认为这可行的列表中。
input: print(list0[:][1])
输入:
print(list0[:][1])
output: [b, .3, .8]
output:
[b, .3, .8]
but I was hoping to get this instead: (.5, .3, .7, 1.03)
但我希望得到这个:
(.5, .3, .7, 1.03)
Is there a possible way to call all sublists and access their elements without looping through whole list to create a new one?有没有一种方法可以调用所有子列表并访问它们的元素,而无需遍历整个列表来创建新列表? Can you compare times and describe why we can't call all or a range of sublists to get an element from each without looping through twice, once to get sublists and once to access each of them?
您能否比较时间并描述为什么我们不能调用所有或一系列子列表来从每个子列表中获取一个元素而不循环两次,一次获取子列表,一次访问每个子列表?
Just use a list comprehension -只需使用列表理解 -
second_element_of_each_sublist = [x[1] for x in list0]
or alternatively, use a map
coupled with itemgetter
-或者,使用
map
与itemgetter
-
from operator import itemgetter
second_element_of_each_sublist = list(map(itemgetter(1), list0))
Avoiding loops altogether isn't gonna happen, at least if you take into account what might be going on behind the scenes.完全避免循环不会发生,至少如果你考虑到幕后可能发生的事情。 It's just a question of efficiency...
就是效率问题。。。
In [196]: list0 = [['a', .5, .3], ['b', .3, .8], ['c', .7, 1.3], ['d', 1.03, .2]]
Some comparative times:一些比较时间:
The recommended list comprehension:推荐的列表理解:
In [197]: timeit [l[1] for l in list0]
410 ns ± 17.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Starting with a list version of 'transpose':从“转置”的列表版本开始:
In [198]: timeit list(zip(*list0))[1]
661 ns ± 3.63 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Selecting a column from an array:从数组中选择一列:
In [199]: timeit np.array(list0)[:,1]
16 µs ± 177 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
The reason numpy
questions often ask "without loops", is that Python level iteration on numpy arrays is slow. numpy
问题经常问“没有循环”的原因是 Python 级别迭代在 numpy ZA3CBC3F9D0CE2DF2C156 上是慢的。 Where possible we want to use the fast compiled numpy code (which still has loops).在可能的情况下,我们希望使用快速编译的 numpy 代码(仍然有循环)。 But when starting from a list, creating the array is relatively expensive.
但是从列表开始时,创建数组的成本相对较高。
Starting with an array, column indexing is fast:从数组开始,列索引很快:
In [200]: %%timeit A = np.array(list0)
...: A[:,1]
325 ns ± 11.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Iteration on an array is slower than the same iteration on a list:数组上的迭代比列表上的相同迭代慢:
In [201]: %%timeit A = np.array(list0)
...: [a[1] for a in A]
5.47 µs ± 96.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.