Notice
Recent Posts
Recent Comments
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags
more
Archives
Today
Total
관리 메뉴

CHIqueen

파이썬에서 dict로 Hash map을 만들때 hash()를 써야할까? 본문

프로그래밍/Python

파이썬에서 dict로 Hash map을 만들때 hash()를 써야할까?

CHIqueen 2020. 4. 24. 20:09

https://www.laurentluce.com/posts/python-dictionary-implementation/

 

Python dictionary implementation | Laurent Luce's Blog

August 29, 2011 This post describes how dictionaries are implemented in the Python language. Dictionaries are indexed by keys and they can be seen as associative arrays. Let’s add 3 key/value pairs to a dictionary: >>> d = {'a': 1, 'b': 2} >>> d['c'] = 3 >

www.laurentluce.com

글을 읽어보는데 2011년도 글이여서 지금은 어떨지 궁금해 코드를 읽어보려고 한다.

 

https://github.com/python/cpython/blob/master/Objects/dictobject.c

 

python/cpython

The Python programming language. Contribute to python/cpython development by creating an account on GitHub.

github.com

hash = PyObject_Hash(key)

PyDict_GetItem, PyDict_SetItem, PyDict_DelItem, PyDict_Contains등 에서 찾아볼 수 있는데

int PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
{
    Py_hash_t hash;
    assert(key);
    if (!PyUnicode_CheckExact(key) ||
        (hash = ((PyASCIIObject *) key)->hash) == -1)
    {
        hash = PyObject_Hash(key);
        if (hash == -1)
            return -1;
    }
}

 

앞뒤 다 짤라먹고 가져왔다. 내부적으로 다 해주는거 같다.

 

평소에 hashmap[hash(var)]=~ 이렇게 해줬는데 오늘 https://programmers.co.kr/learn/courses/30/lessons/42577

이 문제를 풀다가 다른 코드들 보다 조금더 걸리길래 다른분들께 물어봤더니 내부에서 해주는거 아니냐고 왜 hash하셨습니까 해서 hash를 포기했더니 잘 조금 빨라졌습니다

 

https://gist.github.com/tjd5526/99e6b583d829f411ac87ef4c1be75f05

 

전화번호목록

전화번호목록. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

'프로그래밍 > Python' 카테고리의 다른 글

TJCTF 2020 Zipped Up  (0) 2020.05.27
프로그래머스 전화번호 목록  (0) 2020.04.24
UMDCTF Fragile Foundations  (0) 2020.04.23
python 자료형별 시간복잡도  (0) 2020.04.11
백준 4344 평균은 넘겠지 한줄 풀이  (0) 2020.04.09
Comments