2017-11-29 2 views

답변

0

당신은 첫 번째 항목을 건너 뛰어 슬라이스 시작해야합니다; 코드에서

>>> list(i) 
[0, 1, 2, 3, 4, 5, 6, 7, 8] 
>>> list(i[1::2]) 
[1, 3, 5, 7, 9] 

: 예를 들면 다음과 같습니다

content = [x.strip() for x in content[1::2]] 
1

슬라이스에 시작 번호를 추가해야합니다 (예 : 1). content[1::2] :

with open(path) as f: 
    content = f.readlines() 

content = [x.strip() for x in content[1::2]] 

더 나은 대안은 다음과 같이이 작업을 수행하는 itertools.islice()을 사용하는 것입니다 :

from itertools import islice 

with open(path) as f_input: 
    content = [line.strip() for line in islice(f_input, 1, None, 2)] 
-1

코드는 슬라이스가 두 단계로리스트의 말미에 1에서 시작한다는 것을 의미하는 같아야합니다 . 내가 그것을 받아 들일 수 있도록

with open(path) as f: 
    content = f.readlines() 

content = [x.strip() for x in content[1::2]] 
from itertools import islice 

with open(path) as f_input: 
    content = [line.strip() for line in islice(f_input, 1, None, 2)] 
관련 문제