줄임표 […]는 목록에서 무엇을 의미합니까?
나는 파이썬에서 놀고있었습니다. 유휴 상태에서 다음 코드를 사용했습니다.
p = [1, 2]
p[1:1] = [p]
print p
결과는 다음과 같습니다.
[1, [...], 2]
이게 뭐야 […]
? 흥미롭게도 나는 이것을 무한대까지의 목록 목록으로 사용할 수 있습니다.
p[1][1][1]....
원하는만큼 위의 내용을 쓸 수 있으며 여전히 작동합니다.
편집하다:
- 메모리에 어떻게 표현됩니까?
- 그 용도는 무엇입니까? 유용한 경우의 예가 도움이 될 것입니다.
- 공식 문서에 대한 링크는 정말 유용합니다.
내부에 중첩 된 무한 목록을 작성 했으므로 인쇄 할 수 없습니다. p
포함 p
포함하는 p
등등 ... 그리고. [...]
표기법은 당신이 알고, 그것이 표현 될 수 없음을 알려 할 수있는 방법입니다! @ 6502의 답변을보고 무슨 일이 일어나고 있는지 보여주는 멋진 그림을보십시오.
이제 편집 후 세 가지 새로운 항목에 대해
- 이 답변 은 그것을 커버하는 것 같습니다
- Ignacio의 링크 는 몇 가지 가능한 용도를 설명합니다
- 이것은 프로그래밍 언어보다 데이터 구조 설계의 주제이므로 파이썬의 공식 문서에서 참조가 발견되지는 않습니다.
이것이 코드가 만든 것입니다
첫 번째 요소와 마지막 요소가 두 개의 숫자 (1과 2)를 가리키고 중간 요소가 목록 자체를 가리키는 목록입니다.
일반 리스프에서 원형 구조 인쇄가 활성화되면 이러한 개체는 다음과 같이 인쇄됩니다.
#1=#(1 #1# 2)
즉 #1=
, 3 개의 요소가있는 벡터 인 객체 (1로 레이블이 붙어 있음)가 있고, 두 번째는 객체 자체입니다 (와 역 참조 #1#
)
파이썬에서는 대신 구조가 원형이라는 정보를 얻습니다 [...]
.
이 특정 경우 설명은 모호하지 않습니다 (역순으로 목록을 가리 키지 만 하나의 목록 만 있으므로 해당 목록이어야 함). 그러나 다른 경우에는 모호 할 수 있습니다 ... 예를 들어
[1, [2, [...], 3]]
역방향 참조는 외부 또는 내부 목록을 가리킬 수 있습니다. 동일한 방식으로 인쇄 된이 두 가지 구조는
x = [1, [2, 3]]
x[1][1:1] = [x[1]]
y = [1, [2, 3]]
y[1][1:1] = [y]
print(x)
print(y)
그리고 그들은 메모리에있을 것입니다
"무엇을 사용합니까?"라는 질문에 구체적인 예가 있습니다.
그래프 축소 는 컴퓨터 언어를 해석하기 위해 언젠가 사용되는 평가 전략입니다. 이는 특히 기능적 언어의 게으른 평가를위한 일반적인 전략입니다.
The starting point is to build a graph representing the sequence of "steps" the program will take. Depending on the control structures used in that program, this might lead to a cyclic graph (because the program contains some kind of "forever" loop -- or use recursion whose "depth" will be known at evaluation time, but not at graph-creation time)...
In order to represent such graph, you need infinite "data structures" (sometime called recursive data structures), like the one you noticed. Usually, a little bit more complex though.
If you are interested in that topic, here is (among many others) a lecture on that subject:
http://undergraduate.csse.uwa.edu.au/units/CITS3211/lectureNotes/14.pdf
We do this all the time in object-oriented programming. If any two objects refer to each other, directly or indirectly, they are both infinitely recursive structures (or both part of the same infinitely recursive structure, depending on how you look at it). That's why you don't see this much in something as primitive as a list -- because we're usually better off describing the concept as interconnected "objects" than an "infinite list".
You can also get ...
with an infinitely recursive dictionary. Let's say you want a dictionary of the corners of a triangle, where each value is a dictionary of the other corners connected to that corner. You could set it up like this:
a = {}
b = {}
c = {}
triangle = {"a": a, "b": b, "c": c}
a["b"] = b
a["c"] = c
b["a"] = a
b["c"] = c
c["a"] = a
c["b"] = b
Now if you print triangle
(or a
or b
or c
for that matter), you'll see it's full of {...}
because any two corners are referring to back to each other.
As I understood, this is an example of fixed point
p = [1, 2]
p[1:1] = [p]
f = lambda x:x[1]
f(p)==p
f(f(p))==p
The name of that special object is the Ellipsis. I guess that it's implemented as a singleton object in the Python intepreter/VM -- something like None --- a sentinel of sorts. As you've seen it's a way for Python to represent the reference of a list within itself.
참고 URL : https://stackoverflow.com/questions/17160162/what-do-ellipsis-mean-in-a-list
'Programing' 카테고리의 다른 글
클래스에 동적으로 속성을 추가하는 방법은 무엇입니까? (0) | 2020.05.14 |
---|---|
모든 하위 디렉토리의 모든 파일을 bash에서 하나의 압축 파일로 압축하는 방법 (0) | 2020.05.14 |
원형 선분 충돌 탐지 알고리즘? (0) | 2020.05.14 |
Visual Studio 2012/2013/2015/2017/2019에서 매크로를 기록 / 재생할 수 있습니까? (0) | 2020.05.14 |
innerHTML로 스크립트를 삽입 할 수 있습니까? (0) | 2020.05.13 |