从列表或元组中明确选择项目
- 2025-03-18 08:56:00
- admin 原创
- 59
问题描述:
我有以下 Python 列表(也可以是元组):
myList = ['foo', 'bar', 'baz', 'quux']
我可以说
>>> myList[0:3]
['foo', 'bar', 'baz']
>>> myList[::2]
['foo', 'baz']
>>> myList[1::2]
['bar', 'quux']
如何明确地挑选出索引没有特定模式的项目?例如,我想选择[0,2,3]
。或者从一个包含 1000 个项目的大列表中,我想选择[87, 342, 217, 998, 500]
。是否有一些 Python 语法可以做到这一点?看起来像这样:
>>> myBigList[87, 342, 217, 998, 500]
解决方案 1:
list( myBigList[i] for i in [87, 342, 217, 998, 500] )
我将答案与python 2.5.2进行了比较:
19.7 微秒:
[ myBigList[i] for i in [87, 342, 217, 998, 500] ]
20.6微秒:
map(myBigList.__getitem__, (87, 342, 217, 998, 500))
22.7 微秒:
itemgetter(87, 342, 217, 998, 500)(myBigList)
24.6 微秒:
list( myBigList[i] for i in [87, 342, 217, 998, 500] )
请注意,在 Python 3 中,第 1 个被更改为与第 4 个相同。
numpy.array
另一个选择是从允许通过列表或以下方式进行索引开始numpy.array
:
>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
其tuple
工作方式与切片不同。
解决方案 2:
那这个呢:
from operator import itemgetter
itemgetter(0,2,3)(myList)
('foo', 'baz', 'quux')
解决方案 3:
也许列表理解是有序的:
L = ['a', 'b', 'c', 'd', 'e', 'f']
print [ L[index] for index in [1,3,5] ]
生成:
['b', 'd', 'f']
这就是你要找的东西吗?
解决方案 4:
它不是内置的,但如果您愿意,可以创建一个以元组作为“索引”的列表子类:
class MyList(list):
def __getitem__(self, index):
if isinstance(index, tuple):
return [self[i] for i in index]
return super(MyList, self).__getitem__(index)
seq = MyList("foo bar baaz quux mumble".split())
print seq[0]
print seq[2,4]
print seq[1::2]
印刷
foo
['baaz', 'mumble']
['bar', 'quux']
解决方案 5:
>>> map(myList.__getitem__, (2,2,1,3))
('baz', 'baz', 'bar', 'quux')
如果您想要这样做,您还可以创建自己的List
类,该类支持元组作为参数。__getitem__
`myList[(2,2,1,3)]`
解决方案 6:
我只是想指出,即使 itemgetter 的语法看起来很简洁,但在大型列表上执行时速度有点慢。
import timeit
from operator import itemgetter
start=timeit.default_timer()
for i in range(1000000):
itemgetter(0,2,3)(myList)
print ("Itemgetter took ", (timeit.default_timer()-start))
Itemgetter 占用 1.065209062149279
start=timeit.default_timer()
for i in range(1000000):
myList[0],myList[2],myList[3]
print ("Multiple slice took ", (timeit.default_timer()-start))
多片切片耗时 0.6225321444745759
解决方案 7:
另一个可能的解决方案:
sek=[]
L=[1,2,3,4,5,6,7,8,9,0]
for i in [2, 4, 7, 0, 3]:
a=[L[i]]
sek=sek+a
print (sek)
解决方案 8:
就像通常当你有一个布尔 numpy 数组时一样mask
[mylist[i] for i in np.arange(len(mask), dtype=int)[mask]]
适用于任何序列或 np.array 的 lambda:
subseq = lambda myseq, mask : [myseq[i] for i in np.arange(len(mask), dtype=int)[mask]]
newseq = subseq(myseq, mask)
解决方案 9:
以下是一行 lambda:
list(map(lambda x: mylist[x],indices))
在哪里:
mylist=['a','b','c','d','e','f','g','h','i','j']
indices = [3, 5, 0, 2, 6]
输出:
['d', 'f', 'a', 'c', 'g']
解决方案 10:
从此列表中:
myList = ['foo', 'bar', 'baz', 'quux']
使用此代码选择第一、第三和第四个元素:
myList[0:1]+myList[1:2]+myList[2:3]
我发现这是最简单、最直接的方法。
扫码咨询,免费领取项目管理大礼包!