문제 설명
목록 목록의 효과적인 구현 (Effective implementation of list of lists)
여기서 목록 목록을 구현하려고 합니다.
- 외부 목록의 크기가 알려져 있습니다(예: N)
- 각 요소 목록이 다르고 런타임에 결정됩니다.
이를 순진하게 구현하면 다음과 같을 수 있습니다.
L = [[] for x in range(N)]
그리고 L[i] 데이터가 다음과 같이 입력됩니다.
L[i] = range(M)
그러나 요소 목록이 현재 위치에 맞지 않는 경우 파이썬은 전체 목록을 새 위치에 복사해야 하므로 메모리가 매우 무거워집니다.
솔루션이 외부 목록에 연결 목록을 사용한다고 생각하지만 Python에 미리 정의된 연결 목록 구현이 없기 때문에 다른 솔루션이 있는지 궁금합니다.
참조 솔루션
방법 1:
Python doesn't store the sublists inside the outer list; it only stores references to the sublists inside the outer list. Reassigning an inner list will never require the outer list to be copied; at the implementation level, it's little more than a single pointer assignment.
If you're familiar with C# or Java, the terminology you'd be familiar with is that everything is a reference type in Python. If you're familiar with C++, think of it as how resizing an inner vector in a vector<vector<int>>
wouldn't resize the outer vector.
(by MBZ、user2357112)
참조 문서