我如何知道某个数字是否在 PARI/GP 中的列表中?
How can I know if a number is inside a list in PARI/GP?
我想知道一个数字是否在 PARI/GP 列表中,但我不知道该怎么做,这是我的代码:
mylist = listcreate();
... here I add some numbers with listput(mylist,XXXX)
/* How can I do the condition in the if... */
if(mynumber in mylist,print("SUCCESS"),print("ERROR"))
我正在从 Python 迁移到 PARI/GP 我的一些脚本,但我迷失了那些基本的东西,the manual 有点难以理解。谢谢!
您可以像这样测试给定值是否在列表、向量或列向量中:
inList(list, value)=for(i=1,#list, if(list[i]==value, return(i))); 0
或效率低于
inlist(list, value)=#select(n->n==value, list) > 0
你的例子看起来像
if(inList(mylist, mynumber), print("SUCCESS"), print("ERROR"))
但如果您要进行大量查询,则值得使用二分搜索:
myset = Set(mylist);
if(setsearch(myset, mynumber), print("SUCCESS"), print("ERROR"))
我想知道一个数字是否在 PARI/GP 列表中,但我不知道该怎么做,这是我的代码:
mylist = listcreate();
... here I add some numbers with listput(mylist,XXXX)
/* How can I do the condition in the if... */
if(mynumber in mylist,print("SUCCESS"),print("ERROR"))
我正在从 Python 迁移到 PARI/GP 我的一些脚本,但我迷失了那些基本的东西,the manual 有点难以理解。谢谢!
您可以像这样测试给定值是否在列表、向量或列向量中:
inList(list, value)=for(i=1,#list, if(list[i]==value, return(i))); 0
或效率低于
inlist(list, value)=#select(n->n==value, list) > 0
你的例子看起来像
if(inList(mylist, mynumber), print("SUCCESS"), print("ERROR"))
但如果您要进行大量查询,则值得使用二分搜索:
myset = Set(mylist);
if(setsearch(myset, mynumber), print("SUCCESS"), print("ERROR"))