티스토리 뷰
파이썬을 사용하다가 가끔 "리스트안에 특정 값이 있는지"를 확인하는 로직이 들어가는 경우가 있습니다.
평소에는 아래와 같이 사용을 하고 있습니다.
7 in num_list
근데 리스트의 크기가 매우 커지면서 해당 로직에 소요되는 시간이 엄청 길어졌습니다.
뭔가 다른 방법이 없을까? 하다가 검색해보니 이미 깔끔하게 정리해주신 분들이 계셔서 링크를 첨부합니다.
https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list
Fastest way to check if a value exists in a list
What is the fastest way to know if a value exists in a list (a list with millions of values in it) and what its index is? I know that all values in the list are unique as in this example. The first
stackoverflow.com
7 in num_list
7 in set(num_list)
해당 링크의 내용을 보면 제가 평소에 사용하던 리스트를 그대로 사용하는 것이 아니라 set타입으로 변경 후에 사용하는 것이 좀 더 효율적이라는 내용입니다. 시간 소요 그래프 까지 포함되어 있으니 관심 있으신 분들은 실행해보시는것도 좋을거 같습니다.
'개발 일반' 카테고리의 다른 글
[Pandas] read_csv에서는 꼭 dtype을 정의하자 (0) | 2020.05.15 |
---|---|
[Python] __pychache__ 에 대해서 (0) | 2020.05.12 |
[Python] "killed" 명령어 원인 확인하기 (0) | 2020.04.20 |
[python] csv를 json 포맷으로 바꾸기 (0) | 2020.01.06 |
[python] urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)> 에러 해결 (0) | 2019.12.06 |
댓글