Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List - Fatal编程技术网

Python 查找列表中包装给定值的项的位置

Python 查找列表中包装给定值的项的位置,python,list,Python,List,我有一个按升序排列的向量X值,如下例所示: x1 = [1, 5, 7, 9, 13, 17, 24, 30, 35, 46, 51, 60] 我想得到向量中包含给定数字的值的位置,如二进制列表或直接位置: E.g: Number_N = 10 --> xpos=[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0] // xpos=[3,4] Number_N = 50 --> xpos=[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]

我有一个按升序排列的向量X值,如下例所示:

x1 = [1, 5, 7, 9, 13, 17, 24, 30, 35, 46, 51, 60]
我想得到向量中包含给定数字的值的位置,如二进制列表或直接位置:

E.g:

Number_N = 10 --> xpos=[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0] // xpos=[3,4]
Number_N = 50 --> xpos=[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0] // xpos=[9,10]
Number_N = 1 --> xpos=[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] // xpos=[0,1]
我在这里得到一个语法错误:

Xpos = [1 if (l <= num & l+1 >= num) else 0 for l in x1[0:]]
Xpos=[1 if(l=num)else 0表示x1中的l[0:]
这是一个应用程序,它在已排序的集合中查找搜索值的任何现有条目之后的插入点

如果
n
是我们的输入号码,那么我们可以执行以下操作:

idx = bisect.bisect_right(x1, 0)
然后

xpos = [0] * len(x1)
xpos[idx] = 1
if idx > 0:
    xpos[idx - 1] = 1

你尝试过什么?类似这样的东西:Xpos=[1如果(l=num)在x1[0:]中为l取0,请某人帮你做作业?是的,我可以在这里工作!我不知道算法,。谢谢!