我有两个列表a
和b
。 a
包含我想知道b
中匹配元素索引的元素。在b
中,与a
不同,每个元素都是唯一的。
a = [1993, 1993, 1994, 1995, 1996, 1996, 1998, 2003, 2005, 2005]
b = [1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014]
使用Finding the indices of matching elements in list in Python中的解决方案:
matching = [match for match, element in enumerate(b) if element in a]
matching
但只是[27, 28, 29, 30, 32, 37, 39]
,但我希望它是[27, 27, 28, 29, 30, 30, 32, 37, 39, 39]
。
答案 0 :(得分:6)
>>> a = [1993, 1993, 1994, 1995, 1996, 1996, 1998, 2003, 2005, 2005]
>>> b = [1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014]
>>> [b.index(x) for x in a]
[27, 27, 28, 29, 30, 30, 32, 37, 39, 39]
答案 1 :(得分:1)
怎么样?
print [b.index(i) for i in a if i in b]
答案 2 :(得分:1)
这是对Padraic Cunningham suggestion to use sets的扩展。相反,如果您将列表转换为字典,则可以实现O(1)查找,以进行O(n)预处理:
a = [1993, 1993, 1994, 1995, 1996, 1996, 1998, 2003, 2005, 2005]
b = [1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014]
d = {value: index for index, value in enumerate(b)}
print([d[x] for x in a])
>>> timeit("[bisect_left(b, x) for x in a]", "from __main__ import a, b; from bisect import bisect_left")
3.513558427607279
>>> timeit("[b.index(x) for x in a]", "from __main__ import a, b")
8.010070997323822
>>> timeit("d = {value: index for index, value in enumerate(b)}; [d[x] for x in a]", "from __main__ import a, b")
5.5277420695707065
>>> timeit("[d[x] for x in a]", "from __main__ import a, b, ;d = {value : index for index, value in enumerate(b)}")
1.1214096146165389
因此,如果您对预处理进行折扣,那么在实际处理中使用b.index
的速度几乎快8倍 - 如果您正在执行大量列表a
,这会更好反对较少的b
。如果您只执行一次,则使用bisect_left
会更快,并且可以保证b
单调递增。
答案 3 :(得分:0)
如果你有大型列表制作b,那么效率会更高:
a = [1993, 1993, 1994, 1995, 1996, 1996, 1998, 2003, 2005, 2005]
b = [1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014]
from bisect import bisect_left
print [bisect_left(b, x) for x in a]
[27, 27, 28, 29, 30, 30, 32, 37, 39, 39]
当您的数据被排序并假设a中的所有元素都在b中时,您也可以使用bisect,因此每个索引查找都是O(log n):
In [22]: timeit [bisect_left(b, x) for x in a]
100000 loops, best of 3: 4.2 µs per loop
In [23]: timeit [b.index(x) for x in a]
100000 loops, best of 3: 8.84 µs per loop
在小型数据集上,它的运行速度是索引的两倍:
# store all indexes as values and years as keys
indexes = {k: i for i, k in enumerate(b)}
# one pass over a accessing each index in constant time
print [indexes[x] for x in a]
[27, 27, 28, 29, 30, 30, 32, 37, 39, 39]
另一个选择是使用dict来存储索引,这意味着代码将以线性时间运行,一次通过a,一次通过b:
In [34]: %%timeit
indexes = {k: i for i, k in enumerate(b)}
[indexes[x] for x in a]
....:
100000 loops, best of 3: 7.54 µs per loop
In [39]: b = list(range(1966,2100))
In [40]: samp = list(range(1966,2100))
In [41]: a = [choice(samp) for _ in range(100)]
In [42]: timeit [b.index(x) for x in a
10000 loops, best of 3: 154 µs per loop
In [43]: %%timeit
indexes = {k: i for i, k in enumerate(b)}
[indexes[x] for x in a]
....:
10000 loops, best of 3: 22.5 µs per loop
即使在小输入集上,它也比索引更有效率,而且随着a的增长效率会更高:
{{1}}