简体   繁体   English

从多维列表的每个子列表中访问给定元素而不使用 for 循环

[英]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 -或者,使用mapitemgetter -

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.

相关问题 Django-在for循环中访问列表的子列表 - Django - Accessing sublist of list in for loop 用子列表的给定元素将列表中的子列表集分开 - Separate a set of sublists from within a list by a given element of a sublist 从列表中删除子列表(如果包含给定元素) - Removing sublist from list if it contains given elements 创建一个列表列表,其中每个子列表包含来自输入列表序列的一个元素 - Creating a list of lists where each sublist contains one element from an input sequence of lists 根据另一个列表从子列表中删除元素 - Remove element from sublist according to another list 如何制作一个列表以拆分成每个元素并创建一个子列表 - How to make a list to split into each element and make a sublist 如果pandas系列的值是一个列表,如何获取每个元素的子列表? - If the value of pandas series is a list, how to get a subList of each element? Python:访问特定的子列表元素 - Python: Accessing specific sublist element 从列表中选择子列表,其中子列表的项与给定值的差异最小 - Select sublist from a list where sublist's item has minimum difference from a given value 将列表中的一个元素插入另一个列表的第 k 个 position 每个子列表(插入 k+=1 后从左到右)- Python - Insert an element in a list into kth position each sublist (from left to right after inserting k+=1) of another list- Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM