티스토리 뷰

반응형

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

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

 

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

How do I get the number of elements in a list (length of a list) in Python?

파이썬에서 리스트 길이를 어떻게 구할 수 있나요?

 문제 내용 

How do I get the number of elements in the list items?

리스트 항목의 요소 수(리스트 길이)를 어떻게 구할 수 있나요?
items = ["apple", "orange", "banana"]

# There are 3 items.

 

 

 높은 점수를 받은 Solution 

The len() function can be used with several different types in Python - both built-in types and library types. For example:

len() 함수는 파이썬에서 여러 가지 타입 - 내장 타입과 라이브러리 타입 -과 함께 사용할 수 있습니다. 예를 들어:
>>> len([1, 2, 3])
3

 

 

 가장 최근 달린 Solution 

There are three ways that you can find the length of the elements in the list. I will compare the 3 methods with performance analysis here.

리스트의 요소 수를 찾는 세 가지 방법이 있습니다. 여기서 3가지 방법을 성능 분석과 함께 비교해 보겠습니다.

 

Method 1: Using len()

방법 1: len() 사용
items = []
items.append("apple")
items.append("orange")
items.append("banana")

print(len(items))

 

output:

출력:
3

 

Method 2: Using Naive Counter Method

방법 2: Naive Counter 방법 사용
items = []
items.append("apple")
items.append("orange")
items.append("banana")

counter = 0
for i in items:
    counter = counter + 1

print(counter)

 

output:

출력:
3

 

Method 3: Using length_hint()

방법 3: length_hint() 사용
items = []
items.append("apple")
items.append("orange")
items.append("banana")

from operator import length_hint
list_len_hint = length_hint(items)
print(list_len_hint)

 

output:

출력:
3

 

Performance Analysis – Naive vs len() vs length_hint()

성능 분석 – Naive vs len() vs length_hint()

 

Note: In order to compare, I am changing the input list into a large set that can give a good amount of time difference to compare the methods.

참고: 비교하기 위해 입력 리스트를 시간 차이를 비교할 수 있는 큰 집합으로 변경합니다.
items = list(range(100000000))

# Performance Analysis
from operator import length_hint
import time

# Finding length of list
# using loop
# Initializing counter

start_time_naive = time.time()
counter = 0
for i in items:
    # incrementing counter
    counter = counter + 1
end_time_naive = str(time.time() - start_time_naive)

# Finding length of list
# using len()
start_time_len = time.time()
list_len = len(items)
end_time_len = str(time.time() - start_time_len)

# Finding length of list
# using length_hint()
start_time_hint = time.time()
list_len_hint = length_hint(items)
end_time_hint = str(time.time() - start_time_hint)

# Printing Times of each
print("Time taken using naive method is : " + end_time_naive)
print("Time taken using len() is : " + end_time_len)
print("Time taken using length_hint() is : " + end_time_hint)

 

Output:

출력:
Time taken using naive method is : 7.536813735961914
Time taken using len() is : 0.0
Time taken using length_hint() is : 0.0

 

Conclusion

결론

 

It can be clearly seen that time taken for naive is very large compared to the other two methods, hence len() & length_hint() is the best choice to use.

Naive의 소요 시간이 다른 두 방법에 비해 매우 크므로 len()과 length_hint()를 사용하는 것이 최선입니다.

 

 

 

출처 : https://stackoverflow.com/questions/1712227/how-do-i-get-the-number-of-elements-in-a-list-length-of-a-list-in-python

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