該函數搜索數組中元素的數量,並返回數組中元素的數量(如果該數組存在),或者返回-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? –
即時通訊使用python 3.6 –