如何“切片"一对基于其中一个值的列表 [英] How to "slice" a pair of lists based on values in one of them
问题描述
我有两个长度相等的列表,一个包含labels
,另一个包含data
.例如:
I have two lists of equal length, one containing labels
and the other data
. For example:
labels = ['cat', 'cat', 'dog', 'dog', 'dog', 'fish', 'fish', 'giraffe', ...]
data = [ 0.3, 0.1, 0.9, 0.5, 0.4, 0.3, 0.2, 0.8, ... ]
如何基于labels
列表中的特定标签并行提取两个列表的子列表?
How can I extract sub-lists of both lists in parallel based on a particular label in the labels
list?
例如,使用fish
作为选择标准,我要生成:
For example, using fish
as a selection criteria, I want to generate:
selected_labels = [ 'fish', 'fish' ]
selected_data = [ 0.3, 0.2 ]
我的最佳猜测听起来很麻烦-制作一个元素式元组列表,从该列表中提取一个相关元组列表,然后将该元组列表去元组化为两个单个元素列表.即使这是解决问题的方法,但我对Python还是一个陌生的人,无法涉足其语法.
My best guess sounds cumbersome - make a list of element-wise tuples, extract a list of relevant tuples from that list, then de-tuple that list of tuples back into two lists of single elements. Even if that's the way to approach it, I'm too new to Python to stumble on the syntax for that.
推荐答案
tuples = (x for x in zip(labels, data) if x[0] == 'fish')
selected_labels, selected_data = map(list, zip(*tuples))
这是如何工作的?
tuples
行生成一个生成器表达式,该生成器将将两个列表放在一起,然后删除所有无趣的内容.第二行再次使用zip,然后根据需要map
将生成的元组放入list
s.
How does this work?
The tuples
line builds a generator expression which zips the two lists together and drops any thing that is uninteresting. The second line uses zip again and then map
s the resulting tuples into list
s as desired.
这具有不构建任何中间数据结构的优势,因此应该相当快且内存高效.
This has the advantage of building no intermediate data structures so should be fairly fast and memory efficient.
labels = ['cat', 'cat', 'dog', 'dog', 'dog', 'fish', 'fish', 'giraffe']
data = [0.3, 0.1, 0.9, 0.5, 0.4, 0.3, 0.2, 0.8]
tuples = (x for x in zip(labels, data) if x[0] == 'fish')
selected_labels, selected_data = map(list, zip(*tuples))
print(selected_labels)
print(selected_data)
结果:
['fish', 'fish']
[0.3, 0.2]
这篇关于如何“切片"一对基于其中一个值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!