2017-01-04 3 views
0
import sys 
string_input = "6\n212 132322\n212 21\n65 56\n32 3\n3232 32\n313 13\n0" 
# a two dimensional array to store points 
points = [] 
for line in string_input.split("\n"): 
    # split the inputed line using space to divide x and y coordinate 
    points_str = line.split(" ") 
    point_coordinate = [] 
    if len(points_str) != 1: 
     for val in points_str: 
      point_coordinate.append(int(val)) 
      points.append(point_coordinate) 
     print(str(points)) 
print(len(points_str)) 

왜 len (points_str)은 1을 반환합니까? 나는 1! = 1이 나머지 코드를 계속 수행하는 이유를 매우 혼란 스럽습니다.len (point_str)이 1을 반환하는 이유는 무엇입니까?

+3

여기서 'points_str'값이 여러 개 있다는 것을 알고 계십니까? – user2357112

+0

예, 길이가 1인지 확인하기 위해 인쇄물 (len (points_str))을 추가했습니다. – Laptic

+0

이전 값 중 하나가 아닌'points_str'에 지정된 최종 값의 길이 만 인쇄합니다. – user2357112

답변

-1

글쎄, 길이가 1이기 때문에. 적어도 첫 번째 반복에서는. points_str은 그래서

첫 번째 라인은, "6"이다 "6", 당신은 정말 디버거를 사용한다 1.

의 길이를 가지고있다.

+0

Im은 디버거에 익숙하지 않습니다. 설명 할 수 있습니까? – Laptic

+0

첫 번째 결과가 아니라 마지막 반복 결과입니다. –

+0

마지막 줄은 0입니다. 디버거를 사용하십시오. – zmbq

0

먼저 루프가 모든 반복을 실행 한 다음 len(points_str)을 인쇄합니다. 이제 points_str은 각 반복에서 새로운 값을 가져 오지만 끝에 인쇄하기 때문에 의 길이는 points_str 인 마지막 값을 할당 받게됩니다. 이것은 string_input.split("\n")의 마지막 요소이며 '0'입니다. '0'의 길이는 참 (즉, 단지 네 개의 구역을 추가한다) 출력을 검사 루프 라인 내부

print(len(points_str)) 

이동 1

시도이다. 또한 그 길이가 아닌 points_str을 인쇄 해보십시오.

관련 문제