この関数は、配列内の要素の番号を検索し、存在する場合は配列の要素の番号を返し、入力番号が配列に存在しない場合は-1を返します。C関数をPython関数に変換する
int iSearch (int st[],int len,int no)
{
int i;
for (i=1;i<=len;i++) //len=lenth of the array , no = the number that we want to search in the array , st[] = the array
if (st[i]==no)
return i;
return -1;
}
私はこの関数のpythonバージョンを書いていますが、私はPythonで配列の代わりにリストを使うので、Pythonで書く方法はわかりません。
私はPythonで以下のコードを書きましたが、それは
def iSearch(list,lenth,no):
x=0
for x in range (lenth):
if (list(x) == no)
return i
else
return -1
あなたは長さを持っている必要はありません! –
あなたはどのバージョンのPythonを使用していますか? –
im使用するpython 3.6 –