简体   繁体   English

有没有更快的方法来提取列表中的元素?

[英]Is there a faster way to extract elements in a list?

suppose I have the list E = [1,2,1,4,5,4] , and I want to extract the 2nd, 3rd and 5th element in E , let A = [2,3,5] , is there a faster way I can get the elements?假设我有列表E = [1,2,1,4,5,4] ,我想提取E中的第二、第三和第五个元素,让A = [2,3,5] ,有没有我可以更快地获取元素吗? I tried E[A] but it doesn't work.我试过E[A]但它不起作用。 Thanks!谢谢!

Try this:尝试这个:

E = [1,2,1,4,5,4]
A = [2,3,5]

nE = [ E[a] for a in A ]

The itemgetter function from the operator module may do what you want:来自operator模块的itemgetter function 可以做你想做的事:

from operator import itemgetter
E = [1,2,1,4,5,4]
A = [2,3,5]
print(itemgetter(*A)(E))

But you might need a different class altogether depending on the specifics of your speed needs.但是您可能需要完全不同的 class,具体取决于您的速度需求的具体情况。

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

相关问题 使用列表中存在的 substring 提取字符串的更快/更好的方法? - Faster/better way to extract string with a substring present in a list? 有没有更快的方法来查找列表列表中两个元素的共现 - Is there a faster way to find co-occurrence of two elements in list of lists 提取至少包含另一个列表项的列表元素的有效方法 - efficient way to extract list elements containing at least a term of another list 从列表中提取不匹配python中条件的元素的最快方法 - Fastest way to extract elements from a list that not matched condition in python 根据以下内容提取列表元素 - Extract list elements based on 如何将列表列表转换为字符串格式(无法找到将列表列表中的元素提取为字符串的方法) - how to convert list of list to string format (unable to find a way to extract the elements in List of list to a string) 从张量提取补丁的更快方法? - Faster way to extract patches from a tensor? 从 PDF 文件中提取文本的更快方法 - Faster way to extract text from PDF file 更快地从图像中提取补丁? - Faster way to extract patches from images? python中是否有更快的方法将字符串拆分为具有一百万个元素的列表中的子列表? - Is there any faster way in python to split strings to sublists in a list with 1million elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM