티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How do I check if a list is empty?

List가 비어 있는지 확인하려면 어떻게 해야 합니까?

 문제 내용 

For example, if passed the following:

예를 들어, 다음을 통과한 경우:

 

a = []

How do I check to see if a is empty?

a가 비어 있는지 확인하려면 어떻게 해야 합니까?

 

 

 

 높은 점수를 받은 Solution 

if not a:
    print("List is empty")

Using the implicit booleanness of the empty list is quite Pythonic.

빈 리스트의 암시적 bool을 사용하는 것은 꽤 파이썬적이다.

 

 

 

 가장 최근 달린 Solution 

What brought me here is a special use-case: I actually wanted a function to tell me if a list is empty or not. I wanted to avoid writing my own function or using a lambda-expression here (because it seemed like it should be simple enough):

여기까지 오게 된 계기는 특별한 사용 사례입니다. 나는 사실 리스트가 비어있는지 아닌지를 알려주는 기능을 원했다. 나는 내 함수를 쓰거나 람다 표현식을 여기에 사용하는 것을 피하고 싶었다.

 

foo = itertools.takewhile(is_not_empty, (f(x) for x in itertools.count(1)))

And, of course, there is a very natural way to do it:

그리고, 물론, 그것을 하는 매우 자연스러운 방법이 있다:

 

foo = itertools.takewhile(bool, (f(x) for x in itertools.count(1)))

Of course, do not use bool in if (i.e., if bool(L):) because it's implied. But, for the cases when "is not empty" is explicitly needed as a function, bool is the best choice.

물론, bool in if(즉, boole(L):)는 암시적이므로 bool을 사용하지 마십시오. 그러나 함수로서 "비워지지 않음"이 명시적으로 필요한 경우에는 부울이 최선의 선택이다.

 

 

 

출처 : https://stackoverflow.com/questions/53513/how-do-i-check-if-a-list-is-empty

반응형
댓글
공지사항
최근에 올라온 글