●iter() 함수는 반복 가능한 객체를 Iterator 객체로 만든다.
이렇게 함으로써 next() 함수를 이용하여 Iterator 객체의 요소를 하나씩 읽어올 수 있게된다.
● iter(), next()
mylist = iter(['1.사과', '2.오렌지', '3.바나나', '4.배', '5.포도'])
x = next(mylist)
print(x) #사과
x = next(mylist)
print(x) #오렌지
next(mylist) #바나나(출력안되고 다음것으로 넘어가지기만 함)
x = next(mylist)
print(x) #배
1.사과
2.오렌지
4.배
● next()
import csv
f = open('weather.csv', 'r', encoding='utf-8')
lines = csv.reader(f)
header = next(lines)
print(header)
f.close()
['지점', '일시', "평균기온('C)", "최저기온('C)", "최고기온('C)"]
import csv
f = open('month_temp.csv', 'r', encoding='utf-8')
lines = csv.reader(f)
print('%s %s %s %s' % ('일자', ' 최저기온', '최고기온', ' 일교차'))
next(lines) # 헤더를 건너뛴다.
for line in lines:
diff = float(line[4]) - float(line[3]) # 일교차 구하기(최고기온-최저기온)
print('%s ||%.1f ||%.1f ||%.1f' % (line[1], float(line[3]), float(line[4]), diff))
f.close()
일자 최저기온 최고기온 일교차
2019-10-01 ||15.7 ||27.4 ||11.7
2019-10-02 ||20.4 ||23.8 ||3.4
2019-10-03 ||19.9 ||27.8 ||7.9
2019-10-04 ||17.8 ||26.9 ||9.1
2019-10-05 ||15.7 ||22.0 ||6.3
2019-10-06 ||14.0 ||22.0 ||8.0
2019-10-07 ||13.4 ||17.9 ||4.5
2019-10-08 ||10.7 ||20.6 ||9.9
2019-10-09 ||6.4 ||19.6 ||13.2
2019-10-10 ||9.5 ||20.2 ||10.7
2019-10-11 ||12.1 ||24.8 ||12.7
2019-10-12 ||14.1 ||23.3 ||9.2
2019-10-13 ||12.5 ||22.9 ||10.4
2019-10-14 ||8.5 ||19.4 ||10.9
2019-10-15 ||7.3 ||19.6 ||12.3
2019-10-16 ||7.9 ||20.0 ||12.1
2019-10-17 ||9.1 ||20.6 ||11.5
2019-10-18 ||10.0 ||21.9 ||11.9
2019-10-19 ||10.4 ||22.2 ||11.8
2019-10-20 ||10.8 ||21.7 ||10.9
2019-10-21 ||9.5 ||23.9 ||14.4
2019-10-22 ||11.1 ||23.0 ||11.9
2019-10-23 ||12.4 ||20.0 ||7.6
2019-10-24 ||14.1 ||23.3 ||9.2
2019-10-25 ||10.7 ||21.7 ||11.0
2019-10-26 ||6.4 ||13.9 ||7.5
2019-10-27 ||3.0 ||16.8 ||13.8
2019-10-28 ||6.3 ||17.4 ||11.1
2019-10-29 ||8.2 ||18.1 ||9.9
2019-10-30 ||3.3 ||17.1 ||13.8
2019-10-31 ||7.3 ||20.4 ||13.1
출처: 예제 중심 파이썬 입문
궁금한 부분이 있으면 댓글 부탁드립니다^^
'• programming language > python' 카테고리의 다른 글
[파이썬 개념] 데이터 분석 4- csv 파일에서 특정 문자 찾기(csv파일첨부) (0) | 2022.07.20 |
---|---|
[파이썬 개념] 데이터 분석 3- csv 파일 쓰기, 파일 생성, 파일 저장, 내용 입력(csv파일첨부) (0) | 2022.07.20 |
[파이썬 개념] 데이터 분석 1- CSV 파일 읽기 (0) | 2022.07.20 |
[파이썬 연습문제] 파이썬 클래스, 객체, 속성, 메소드, 인스턴트, 생성자 (0) | 2022.07.20 |
[파이썬 개념] 클래스, 객체, 속성, 메소드, 인스턴트,생성자 (0) | 2022.07.19 |