Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在有序列表中的点之前、之后和点之间计数_Python_Algorithm - Fatal编程技术网

Python 在有序列表中的点之前、之后和点之间计数

Python 在有序列表中的点之前、之后和点之间计数,python,algorithm,Python,Algorithm,我正在尝试创建一个算法,它将计算有序列表中某些点之前、之后和之间的数。这里我的“某些点”是y值,需要附加到x值并排序。这里有一个简单的例子来说明我要做什么 x = [2,3,5,6,7,9] y = [1,4,10] # appending y to x and ordering gives us: full_list = [1,2,3,4,5,6,7,9,10] 因此,对于y=1,将通过对完整的_列表进行切片,以非编程方式计算度量: num_between_before = len(fu

我正在尝试创建一个算法,它将计算有序列表中某些点之前、之后和之间的数。这里我的“某些点”是y值,需要附加到x值并排序。这里有一个简单的例子来说明我要做什么

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

# appending y to x and ordering gives us: 
full_list = [1,2,3,4,5,6,7,9,10]
因此,对于y=1,将通过对完整的_列表进行切片,以非编程方式计算度量:

num_between_before = len(full_list[0:0])
0
num_between_after = len(full_list[0:4])
2
num_before = len(full_list[0:0])
0
num_after = len(full_list[0:9])
8
当然,我试图以编程的方式为任何列表x和任何列表y做这件事,它们总是数值的。x和y可以是任何尺寸,但len(y) 以编程方式,输出可能如下所示:

# y_val: (num_between_before, num_between_after, num_before, num_after)
output:
1:  (0, 2, 0, 8)
4:  (2, 4, 3, 5)
10: (4, 0, 8, 0)
显然,到目前为止,我在代码中遇到了问题:

x = [1434684599341,1434684606154,1434684607190,1434684613843,1434684677605,
     1434684704358,1434684708727,1434684724495,1434684758413,1434684782632]

y = [1434471725039, 1434684613844, 1434684708728, 1434684782633]

y.sort()

for i in y:
    x.append(i)

x.sort()

idx = []
for j in y:
    idx.append(x.index(j))   

counter = 0

for i, k in zip(idx, y):

    counter += 1
    if i == 0:
        before = len(x[i:i])
        after = len(x[i:conv_index[counter]]) - 1
        print before, after
    elif i == idx[-1]:
        before = len(x[i-counter:idx[counter-1]]) - 1
        after = len(x[i:i])
        print before, after
    else:
        before = len(x[i:idx[counter]])
        after = len(x[i:idx[counter]]) - 1
        print before, after

如果我听你的,我认为这是可行的:

def allCounts(x,y):
    z = x+y
    z.sort()
    d = {}
    for i in y:
        d[i] = z.index(i)
    counts = {}
    for i,j in enumerate(y):
        if i == 0:
            counts[j] = (0,d[y[i+1]]-d[j]-1,d[j],len(z)-d[j]-1)
        elif i < len(y) - 1:
            counts[j] = (d[j]-d[y[i-1]]-1,d[y[i+1]]-d[j]-1,d[j],len(z)-d[j]-1)
        else:
            counts[j] = (d[j]-d[y[i-1]]-1,0,d[j],len(z)-d[j]-1)
    return counts

您可以使用
bisect\u left
bisect\u right
来获得必要的索引:

#!/usr/bin/env python
from bisect import bisect
x = [2,3,5,6,7,9]; x.sort()
y = [1,4,10]; y.sort()

# appending y to x and ordering gives us:
z = sorted(x + y)
x_indices = [0] + [bisect(x, yy) for yy in y] + [len(x)]
z_indices = [1] + [bisect(z, yy) for yy in y] + [len(z)]
for i, yy in enumerate(y):
    print('{}: {}'.format(yy, (x_indices[i+1] - x_indices[i],
                               x_indices[i+2] - x_indices[i+1],
                               z_indices[i+1] - z_indices[0],
                               z_indices[-1] - z_indices[i+1])))
输出
定义num_before、num_before、num_before和num_after。请详细解释您要做的事情。感谢您的建议。我试图更彻底地解释。在第一个示例中,您是如何得到完整列表中的8的?什么时候你想要的结果会不同于将两个列表合并成一个列表,然后对其进行排序?如果没有区别,那就是我会使用的方法。@TrisNefzger啊,是的,你的权利,我的错误,它被纠正了,这是永远不会发生的。我刚刚意识到之前的num_和之后的num_正在计算不应该发生的y值。例如10:(4,0,6,0)而不是10:(4,0,8,0),因为8对插入到新排序列表中的y值进行计数。
#!/usr/bin/env python
from bisect import bisect
x = [2,3,5,6,7,9]; x.sort()
y = [1,4,10]; y.sort()

# appending y to x and ordering gives us:
z = sorted(x + y)
x_indices = [0] + [bisect(x, yy) for yy in y] + [len(x)]
z_indices = [1] + [bisect(z, yy) for yy in y] + [len(z)]
for i, yy in enumerate(y):
    print('{}: {}'.format(yy, (x_indices[i+1] - x_indices[i],
                               x_indices[i+2] - x_indices[i+1],
                               z_indices[i+1] - z_indices[0],
                               z_indices[-1] - z_indices[i+1])))
1: (0, 2, 0, 8)
4: (2, 4, 3, 5)
10: (4, 0, 8, 0)