2017-12-26 2 views

답변

2

: 그것은 내가 일반적인 경우/다른 구조를 알고 있지만 구문이 확실 words

big_list = ['this', 'is', 'a', 'long', 'list', 'of' 'words'] 

needle = ['words', 'to', 'find'] 

for i in big_list: 
    if i in needle: 
     print('Found') 
    else: 
     print('Not Found') 

big_list에 명중 할 때 '찾을'를 인쇄한다 이 :

for (let i = 0; i < big_list.length; i++) 
{ 
    if (needle.includes(big_list[i])) console.log("Found"); 
    else console.log("Not found"); 
} 

또는 forEach 사용 :

big_list.forEach(function(e) { 
    if (needle.includes(e)) console.log("Found"); 
    else console.log("Not found"); 
}); 
1

거의 이미 알아 보았지만 몇 가지 유의 사항이 있습니다. 첫째, in 키워드는 자바 스크립트에서와 파이썬에서 다르게 작동합니다. 파이썬에서는 항목이 컬렉션의 멤버인지 확인하는 데 사용할 수 있지만 자바 스크립트에서는 항목이 의 개체인지 확인합니다. 그래서 :

"foo" in {foo: "bar"} // True 

하지만 "foo" 배열의 구성원 인 반면 두 번째 경우는 in 키워드가 핵심으로 찾고 있기 때문에

"foo" in ["foo", "bar"] // False 

. 그 라인에 : "0" 이후

"0" in ["foo", "bar"] // True 

이보다

다른 배열의 키 (첫 번째 항목, 즉 키 포인팅) 인, 코드가 많이 변경하지 않고 자바 스크립트에 적용 할 수 있습니다. 그냥 if 년대와 var, const, 또는 let를 사용 괄호를 사용하여 변수를 선언하고 자신의 자바 스크립트 등가물로 통화를 대체 :

/* 
 

 
# Python Code: 
 
big_list = ['this', 'is', 'a', 'long', 'list', 'of', 'words'] 
 

 
needle = ['words', 'to', 'find'] 
 

 
for i in big_list: 
 
    if i in needle: 
 
     print('Found') 
 
    else: 
 
     print('Not Found') 
 

 
*/ 
 

 
// Now in javascript: 
 

 
const big_list = ['this', 'is', 'a', 'long', 'list', 'of', 'words'] 
 
const needle = ['words', 'to', 'find'] 
 

 
for (let i of big_list) { 
 
    if (needle.includes(i)) { 
 
    console.log('Found') 
 
    } else { 
 
    console.log('Not Found') 
 
    } 
 
}