python 에서 iterable한 객체를 argument로 받는 함수 종류
페이지 정보
본문
## 출처 : https://supermemi.tistory.com/7
all (x)
: iterable 자료형 x 를 입력 인수로 받으며, x 가 모두 참이면 True, 하나라도 거짓이면 False를 리턴.
```
>>> x = [1,2,3,4]
>>> all(x)
True
>>> x = [0,1,2,3]
>>> all(x)
False
```
any (x)
: iterable 자료형 x 를 입력 인수로 받으며, x 가 하나라도 참이면 True, 모두 거짓이면 False를 리턴.
```
>>> x = [1,2,3,4]
>>> any(x)
True
>>> x = [0,""]
>>> any(x)
False
>>> x = [0,1,2,3]
>>> any(x)
True
```
filter (function, iterable)
: iterable한 자료형을 넣었을때, 첫 번째 함수에서 참인 것만 리턴한다.
```
>>> def positive(x):
... return x > 0
>>> print(list(filter(positive, [1, -2, 3, -4, 5, -6])))
[1, 3, 5]
```
map (function, iterable)
: 입력 받은 iterable 자료형의 각 요소가 함수 f에 의해 수행된 결과를 묶어서 리턴한다.
```
>>> def two_times(x) : return x*2
...
>>> list(map(two_times, [1,2,3,4,5]))
[2, 4, 6, 8, 10]
list(map(lambda x : x+1, [1,2,3,4,5]))
[2, 3, 4, 5, 6]
```
## 출처
https://supermemi.tistory.com/7
all (x)
: iterable 자료형 x 를 입력 인수로 받으며, x 가 모두 참이면 True, 하나라도 거짓이면 False를 리턴.
```
>>> x = [1,2,3,4]
>>> all(x)
True
>>> x = [0,1,2,3]
>>> all(x)
False
```
any (x)
: iterable 자료형 x 를 입력 인수로 받으며, x 가 하나라도 참이면 True, 모두 거짓이면 False를 리턴.
```
>>> x = [1,2,3,4]
>>> any(x)
True
>>> x = [0,""]
>>> any(x)
False
>>> x = [0,1,2,3]
>>> any(x)
True
```
filter (function, iterable)
: iterable한 자료형을 넣었을때, 첫 번째 함수에서 참인 것만 리턴한다.
```
>>> def positive(x):
... return x > 0
>>> print(list(filter(positive, [1, -2, 3, -4, 5, -6])))
[1, 3, 5]
```
map (function, iterable)
: 입력 받은 iterable 자료형의 각 요소가 함수 f에 의해 수행된 결과를 묶어서 리턴한다.
```
>>> def two_times(x) : return x*2
...
>>> list(map(two_times, [1,2,3,4,5]))
[2, 4, 6, 8, 10]
list(map(lambda x : x+1, [1,2,3,4,5]))
[2, 3, 4, 5, 6]
```
## 출처
https://supermemi.tistory.com/7
추천0
댓글목록
등록된 댓글이 없습니다.