[Python] 26. [Scraping] 재귀 호출 함수, Lamda 함수 이용 , random 난수 발생, LX

2017. 8. 5. 00:30·빅데이터 프로그래밍/Python
728x90
반응형
01. 재귀 호출 함수
- 자기자신을 계속 호출하는 로직으로 일반적으로 1000회 이상 반복하면 에러가 발생함으로
   재귀호출을 중지하는 제어문이 필용함.
 
PyDev setting
project name: crawler
 
1. 모든 처리를하고 재귀함수를 호출하는 경우
 
[실행 화면]
recursionLevel: 5
recursionLevel: 4
recursionLevel: 3
recursionLevel: 2
recursionLevel: 1
recursionLevel: 0
 
▷ /basic/recursion1.py
-------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-

def process(recursionLevel):
    print('recursionLevel: ' + str(recursionLevel))
    
    recursionLevel = recursionLevel - 1
    
    if (recursionLevel < 0):
        return
    
    process(recursionLevel) # 자기 자신 호출
    

if __name__ == '__main__':
    process(5)
    
    

-------------------------------------------------------------------------------------
 
 

2. 재귀호출을 먼저하고 로직을 처리하는 경우

 
[실행 화면]
recursionLevel: 0
recursionLevel: 1
recursionLevel: 2
recursionLevel: 3
recursionLevel: 4
 
▷ /basic/recursion2.py
-------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-

def process(recursionLevel):
    recursionLevel = recursionLevel - 1
    if (recursionLevel < 0):
        return
    process(recursionLevel)
    print('recursionLevel: ' + str(recursionLevel))    

if __name__ == '__main__':
    process(5)

-------------------------------------------------------------------------------------
 
 

02. 람다 표현식(익명 함수(lambda function))

- 한줄로 표현하는 함수
- lambda 라는 키워드로 익명 함수를 정의할 수 있다.
- 형식: lambda 인자1,인자2, … : 표현식
- map: list의 요소를 순차적으로 가져올 수 있는 Iterator 객체 생성 
 
[실행 화면]
람다 함수 실습
3
-----------------------------------
<class 'list'>
10
15
20
25
30
-----------------------------------
8
12
16
20
24
-----------------------------------
6
9
12
15
18
 
-----------------------------------
 
 
▷ /basic/lamdaExam.py
-------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-

print('람다 함수 실습')
add = lambda a, b : a+b # 람다 함수 선언
print(add(1,2))
print('-----------------------------------')

x = [2, 3, 4, 5, 6]  # list
y = []  # list
for v in x : # list 요소 순차적 추출 2, 3, 4, 5, 6
    y = y + [v * 5]

print(type(y)) # <class 'list'>

for i in y:
    print(i)

print('-----------------------------------')
x = [2, 3, 4, 5, 6]
y = [v * 4 for v in x]
    
for i in y:
    print(i)
    
print('-----------------------------------')
data = [2, 3, 4, 5, 6]
# map: list의 요소를 순차적으로 가져올 수 있는 Iterator 객체 생성 
# data list에서 su 변수로 원소를 꺼낸후 3을 곱해 map의 요소로 사용
mapresult = map(lambda su : su * 3, data)
    
# print(str(list(mapresult)))

for i in mapresult: # map
    print(i)
     

-------------------------------------------------------------------------------------
 
 
 
03. random 난수 발생
 
[실행 화면]
0.8792920747978517
0.8925141487274022
0.41608222846384024
0.9763061019930674
0.5175325632759177
-----------------------------
6
7
4
2
7
-----------------------------
7
9
7
9
9
-----------------------------
7
8
7
9
7
-----------------------------
-4
4
3
5
-2
-----------------------------
 
 
▷ /basic/randomExam.py
-------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-

import random

for i in range(5): # 0 ~ 4
    print(random.random())  # 0.0 ~ 1.0 미만
print('-----------------------------')

for i in range(5):
    print(random.randint(1, 10))  # 1 ~ 10 
print('-----------------------------')

for i in range(5):
    print(random.randint(0, 5)+5)  # 5 ~ 10 
print('-----------------------------')

for i in range(5):
    print(random.randint(1, 5)+5)  # 6 ~ 10 
print('-----------------------------')

for i in range(5):
    print(random.randint(-80, 0)+40)  # -40 ~ +40 
print('-----------------------------')



-------------------------------------------------------------------------------------
 
 
04. LXML 설치
- 
whl(휠): 파이썬 패키지 파일(Eclipse Plugin과 비슷함)
- 
pip: 파이썬 패키지 관리 툴, 설치 및 삭제, update 지원
- Python에서 XML parser로서 주로 이용되는 패키지
- https://pypi.python.org/pypi/lxml -> lxml 3.7.3
 
1. 다운로드
- lxml-3.7.3-cp36-cp36m-win_amd64.whl (md5)  다운로드
 
2. 설치
F:\201701_python\setup>pip install lxml-3.7.3-cp36-cp36m-win_amd64.whl
Processing f:\201701_python\setup\lxml-3.7.3-cp36-cp36m-win_amd64.whl
Installing collected packages: lxml
Successfully installed lxml-3.7.3
 
F:\201701_python\setup>pip list
DEPRECATION: The default format will switch to columns in the future. You can us
e --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.con
f under the [list] section) to disable this warning.
beautifulsoup4 (4.5.3)
lxml (3.7.3)
pip (9.0.1)
setuptools (28.8.0)
six (1.10.0)
wxPython-Phoenix (3.0.3.dev2902+a79cd32)
 
 
3. 패키지 인식이 안될시 Eclipse 재시작

 

 

728x90
반응형

'빅데이터 프로그래밍 > Python' 카테고리의 다른 글

[Python] 28. [Scraping] 한겨레 신문 뉴스, Naver 뉴스, 동아 일보 뉴스 검색 drawling  (1) 2017.08.16
[Python] 27. [Scraping] Web Scraping 기초, 한글 처리, BeautifulSoup 설치, 기본 트리 운행, 정규 표현식 이용  (0) 2017.08.05
[Python] 25. Google Gmail SMTP 서버를 이용한 Mail 전송  (2) 2017.08.05
[Python] 24. Regular Expression(정규 표현식) 기본 문법 실습 2, Pyperclip library, cx_freeze로 EXE 만들기  (0) 2017.08.05
[Python] 23. Regular Expression(정규 표현식) 기본 문법 실습 1  (0) 2017.08.05
'빅데이터 프로그래밍/Python' 카테고리의 다른 글
  • [Python] 28. [Scraping] 한겨레 신문 뉴스, Naver 뉴스, 동아 일보 뉴스 검색 drawling
  • [Python] 27. [Scraping] Web Scraping 기초, 한글 처리, BeautifulSoup 설치, 기본 트리 운행, 정규 표현식 이용
  • [Python] 25. Google Gmail SMTP 서버를 이용한 Mail 전송
  • [Python] 24. Regular Expression(정규 표현식) 기본 문법 실습 2, Pyperclip library, cx_freeze로 EXE 만들기
밍글링글링
밍글링글링
mingling - 밍글링, 밍글밍글링. 코드와 어우러지다. IT/ 프로그래밍/소스
    반응형
    250x250
  • 밍글링글링
    mingling
    밍글링글링
  • 전체
    오늘
    어제
    • 밍글링글링 (407) N
      • Flutter (2)
      • 일상생활 (8)
        • 리뷰 (1)
        • 생활정보 (4)
        • 맛집 (0)
        • 여행 (0)
        • 모든정보 (3)
      • JAVA (126)
        • 개념 (6)
        • 예제 (115)
        • Exception (2)
      • C (1)
        • C (1)
        • C++ (0)
        • C# (0)
      • JS (29)
        • JavaScript (18)
        • JQuery (5)
        • AJax (0)
        • NODE.JS (6)
        • Angular.JS 2.0 (0)
      • WEB (87)
        • HTML (6)
        • CSS (61)
        • JSP (20)
        • JSTL (0)
      • FrameWork (8)
        • Spring (8)
        • BootStrap (0)
        • MyBATIS (0)
        • JUnit (0)
      • 외부 라이브러리 (5)
      • 공유 소스 관리 (5)
        • Git (5)
        • SVN (0)
      • 빅데이터 프로그래밍 (37)
        • Python (37)
        • R Programming (0)
      • DB (7)
        • ORACLE (0)
        • MySql (6)
      • Development Tools (7)
        • StarUML (0)
        • eXERD (0)
        • Eclipse (4)
      • SKILL (6)
        • Migration (0)
        • Security (6)
      • MicroSoft (0)
        • Excel (0)
        • Word (0)
      • Android (0)
      • Server (21)
        • Ubuntu (5)
        • Linux (15)
      • IOS (0)
      • XML (0)
      • 미디어 (0)
      • 공지사항 (3)
      • NETWORK (1)
      • 게임 (4)
        • 피파 (1)
        • 리니지M (0)
        • 배틀그라운드 (1)
        • 듀랑고 (2)
      • 세상 이슈 (6)
      • 일렉트론 (0)
      • 대회 소식 (4)
      • 업무 (2)
      • Express, Vue (6)
      • docker (11)
      • svelte (3)
      • 블록체인 (1)
      • IT (10) N
      • Rust (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    rust linux
    자바 배열
    ssl 인증서 발급
    gitlab 설치
    css list
    Node
    ubuntu
    css transition
    servlet class
    lang rust
    자바 객체 지향
    svelte
    에디터 팁
    Extension Bisect
    vue 설치
    러스트
    spring java
    css block
    jsp include
    클론코딩
    proxy pass
    vue cli
    오류
    티스토리 자동화
    css float
    React Compiler
    css perspective
    nginx ssl 적용
    nginx
    java casting
    자바 생성자
    docker
    Rust lang
    vscode
    jsp parameter
    자바 클래스
    리눅스 설치
    SSL 인증서
    css tb
    Java Array
    AI 코딩 에이전트
    API 설계
    nginx ssl 설정
    mysql db
    VS Code 팁
    자바 exception
    css table
    자바 for문
    프런트엔드
    브라우저 자동화
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
밍글링글링
[Python] 26. [Scraping] 재귀 호출 함수, Lamda 함수 이용 , random 난수 발생, LX
상단으로

티스토리툴바