python - dict.setdefault

index = dict.serdefault(key,default)

尝试往dict中插入新键值key,如果key已存在就原dict不变,否则插入key:defalut;返回值为key在dict中的下标

可以用来实现稀疏矩阵https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html

>>> docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]]
>>> indptr = [0]
>>> indices = []
>>> data = []
>>> vocabulary = {}
>>> for d in docs:
...     for term in d:
...         index = vocabulary.setdefault(term, len(vocabulary))
...         indices.append(index)
...         data.append(1)
...     indptr.append(len(indices))
...
>>> csr_matrix((data, indices, indptr), dtype=int).toarray()
array([[2, 1, 0, 0],
       [0, 1, 1, 1]])

 

posted @ 2017-04-14 22:28  oO上官麦兜Oo  阅读(191)  评论(0编辑  收藏  举报