티스토리 뷰
1. 지능형 리스트 : 새로운 리스트만 생성, 짧은 코딩
for loop 효율적 사용 방법
폴더 내 파일리스트 불러오기 선택된 파일 load 하기
- 좋은 예
flist = [flist for flist in flist_base if flist.startswith(head_name)]
fpath_base = os.path.join('./data/','sub_dir')
flist_base = os.listdir(fpath_base)
flist = [flist for flist in flist_base if flist.startswith(head_name)]
for i in range(0,len(flist)):
vname = flist[i]
fdata = np.loadtxt(os.path.join(fpath_base,vname), delimiter=",")
- 나쁜 예
flist_sum = []
for flist in flist_base:
if flist.startswith(head_name):
flist_sum.append(flist)
% 차이점
2. 제너레이터 표현식 메모리 유지가 필요없는 데이터 생성
colors = ['black','white']
sizes = ['S','M','L']
tshirts = ('%s %s' % (c,s) for c in colors for s in sizes)
for tshirt in ('%s %s' % (c,s) for c in colors for s in sizes):
print(tshirt,type(tshirt))
print(tshirts)
print(type(tshirts))
print(list(tshirts))
tshirts를 제너레이터 형태로 저장 후 list로 변환하여 사용 가능
제너레이터는 오브젝트 형태로 저장하기 모든 값을 저장하는 리스트 보다 메모리 효율이 좋음
메모리 확인 : sys.getsizeof
반응형
'python > Data Science' 카테고리의 다른 글
(macOS)[python] 파일이름 encoding, 한글 자모분리 해결 (0) | 2022.02.27 |
---|---|
(macOS)[python] 기상청 RSS 서비스를 이용한 데이터 parsing (0) | 2021.02.15 |
(macOS)[python] IP address 확인 : Public/Pravate(Virtual) (0) | 2021.02.15 |
(macOS)[python] crawling, scraping, parsing 용어 - 1 (0) | 2021.02.10 |
(macOS)[python] Class : public, protected, private, inheritance (0) | 2020.03.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Model
- server
- Raspberry Pi
- template
- 코로나19
- COVID-19
- analysis
- 라즈베리파이
- Django
- r
- CSV
- arduino
- Templates
- DS18B20
- 자가격리
- pyserial
- 확진
- ERP
- vscode
- github
- SSH
- 코로나
- Pandas
- Python
- sublime text
- DAQ
- git
- MacOS
- Regression
- raspberrypi
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함