비 - 파이썬 그러나 쉽게 접근 할 수 있습니다.
listCountries = ['France', 'Spain', 'United Kingdom', 'Italy', 'Portugal', 'Ireland', 'Poland', 'Norway']
i=0
l = len(listCountries)
while i<l:
if listCountries[i] == "Spain":
print "Spain found at index", i
break;
else:
i=i+1
또한이 하나
for j in range(len(listCountries)):
if listCountries[j] == "Spain":
print "Spain found at index", j
else:
continue
물론, 당신은 당신을 위해 모든 것을 할 것입니다 코드 아래
def look_for(c, data):
for j in range(len(data)):
if data[j] == c:
return c+" found at index "+str(j)
else:
continue
return c+" not found"
#print look_for("Spain", listCountries) => will return Spain found at index 1
#print look_for("USSR", listCountries) => USSR not found
#print look_for("Finland", listCountries) => Finland not found
, 이러한 기능 내부 조각 위에 랩 수 있습니다 -
print listCountries.index("Spain")
))
정수에서 선형 검색이 그렇게 많은 이유는 무엇입니까? 문자열, 국가 등에서의 선형 검색과 다른 ...? –
프로그래밍에 익숙하지 않다고 말했듯이, 배열이 정수인지 문자인지/문언인지에 따라 다른 코드를 사용한다고 가정했습니다. – ladolcevita
잘 파이썬은 동적 인 언어입니다. 즉, 어떤 것도리스트에 넣을 수 있습니다. 따라서 * 많은 * (모든 경우는 아님)에서 정수 목록에 대해 작동하는 알고리즘은 말, 자동차, 목차 목록에도 적용됩니다. –