티스토리 뷰
<표현식> for <아이템> in <시퀸스타입객체> (if <조건식>) #if문은 생략가능
>>> l = [1,2,3,4,5] #리스트
>>> [i ** 2 for i in l] #리스트의 각객체를 2제곱하여 새로운 리스트객체 생성
[1, 4, 9, 16, 25]
>>> t = ('apple','banana','orange') #튜플
>>> [len(i) for i in t] #각 문자열의 길이를 새로운 새로운 리스트객체로 생성
[5, 6, 6]
>>> d = {100:'apple',200:'banana',300:'orange'} #사전
>>> [v.upper() for v in d.values()] #value값을 대문자로 변환하여 새로운 리스트객체로 생성
['BANANA', 'ORANGE', 'APPLE']
-. if문을 이용하여 조건에 만족하는 아이템만 고르기
>>> L = ['apple', 'orange', 'banana', 'kiwi']
>>> #문자열의 길이가 5를 초과하는 아이템만 골라서 새로운리스트객체로 생성
>>> [i for i in L if len(i) > 5]
['orange', 'banana']
-. 원본리스트가 2개 이상의 다수인 경우, 리스트 내장을 이용하여 리스트의 조합을 만들어낼수 있음
>>> L_1 = [3,4,5]
>>> L_2 = [1,5,-0.5,4]
>>> #L_1과 L_2의 객체를 순환하며 각 아이템들의 곱한값으로 새로운리스트객체 생성
>>> [x * y for x in L_1 for y in L_2]
[3, 15, -1.5, 12, 4, 20, -2.0, 16, 5, 25, -2.5, 20]
'Python' 카테고리의 다른 글
Python, zip() (0) | 2016.04.21 |
---|---|
Python, filter() (0) | 2016.04.21 |
Python, enumerate() (0) | 2016.04.21 |
Python, range() (0) | 2016.04.21 |
Python, 연산자 (0) | 2016.04.21 |
- Total
- Today
- Yesterday
- Linux
- borderwidth
- highlightthickness
- Composite Key
- indicatoron
- Private
- IdClass
- Excel
- 파이썬
- vba
- apache
- ManyToOne
- Java
- FetchType
- disabledforeground
- onetomany
- 리눅스
- activebackground
- 폼
- Python
- JPA
- Module
- tkinter command & bind [명령어묶기와 사건묶기] Python
- highlightbackground
- fetch join
- checkbutton
- tkinter
- activeforeground
- command
- 상수
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |