按索引将列表映射到列表(Python)

时间:2013-05-21 07:56:49

标签: python list mapping binning

我正在寻找一种有效的方法来映射值列表,比如说

x = [1,2,3,4,5,6,7,8,9] 

到另一个列表,其中包含相同总范围的值列表,例如

z = [[1,2,3], [4,5,6], [7,8,9]]. 

我需要知道的基本上是x中的值对应于z的索引。 x[0]=1对应z[0],因为它位于z的第一个bin中,x[7]=8对应z[2],因为它位于第三个bin中。

如何为每个x值识别z的子列表索引?在我的情况下,x有大约50,000个值,z有大约5,000个子列表,我有很多这些列表,所以我正在寻找一种尽可能快的方法。

3 个答案:

答案 0 :(得分:5)

你可以在这里使用词典:

>>> x = [1,2,3,4,5,6,7,8,9]
>>> z = [[1,2,3], [4,5,6], [7,8,9]]
>>> dic = {y :i for i,a in enumerate(z) for y in a}
>>> dic[7]
2
>>> dic[8]
2
>>> dic[1]
0

答案 1 :(得分:0)

>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y = [[1,2,3], [4,5,6], [7,8,9]]
>>> map(lambda x: [i for i in range(len(y)) if y[i].count(x) > 0], x)
[[0], [0], [0], [1], [1], [1], [2], [2], [2]]
>>> 
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y = [[1,2,3], [4,5,6], [7,8,9], [1,2,3]]
>>> map(lambda x: [i for i in range(len(y)) if y[i].count(x) > 0], x)
[[0, 3], [0, 3], [0, 3], [1], [1], [1], [2], [2], [2]]
>>> 
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 9, 7]]
>>> map(lambda x: [i for i in range(len(y)) if y[i].count(x) > 0], x)
[[0, 3], [0], [0], [1], [1], [1], [2, 3], [2], [2, 3]]
>>> 
>>> x = [ 4, 5, 6, 7, 8, 9, 1, 2, 3]
>>> y = [[1, 2, 3], [7, 8, 9], [1, 9, 7], [4, 5, 6]]
>>> map(lambda x: [i for i in range(len(y)) if y[i].count(x) > 0], x)
[[3], [3], [3], [1, 2], [1], [1, 2], [0, 2], [0], [0]]
>>> 

>>> x = [ 4, 5, 6, 7, 8, 9, 1, 2, 3]
>>> y = [[1, 2, 3], [7, 8, 9], [1, 9, 7], [4, 5, 6]]
>>> d = {}
>>> d.update(map(lambda x: (x, [i for i in range(len(y)) if y[i].count(x) > 0]), x))
>>> d
{1: [0, 2], 2: [0], 3: [0], 4: [3], 5: [3], 6: [3], 7: [1, 2], 8: [1], 9: [1, 2]}
>>> 

我认为它有效!

答案 2 :(得分:0)

您似乎在间隔搜索,可以使用间隔树。快速谷歌显示https://github.com/tylerkahn/intervaltree-python,并进行一些修改,我希望它能够非常有效地工作。

我的修改后的模块位于http://pastebin.com/M6rpYt44,只需添加一个顺序属性key,如果它尚不存在。

from IntervalTree import *

x = [1,2,3,4,5,6,7,8,9]
z = [[1,2,3], [4,5,6], [7,8,9]]

intervals = [Interval(l[0],l[-1]) for l in z]
it = IntervalTree(intervals)

# search
[it.search(i)[0].key for i in x]
# [0, 0, 0, 1, 1, 1, 2, 2, 2]