https://www.python.org/static/community_logos/python-logo-inkscape.svg

 

 

 조건 함수를 만족하는 데이터만을 추려냄! 

* 필터 형태로 반환하므로 리스트나 튜플로 변환해주어야 함

 

 

 1) 일반 함수 사용 

friends = [{'name': 'Robert Downey Jr.', 'role': 'Iron Man', 'nationality': 'the United States'},
	    {'name': 'Tom Holland', 'role': 'Spider Man', 'nationality': 'the United Kingdom'}, 
            {'name': 'Chris Evans', 'role': 'Captain America', 'nationality': 'the United States'},
            {'name': 'Chris Hemsworth', 'role': 'Thor', 'nationality': 'Australia'}]

def IsAmerican(data):
	return data["nationality"] == "the United States"
    
filter(IsAmerican, friends) # <filter object at 0x000001E45BF7A550>

list(filter(IsAmerican, friends)) 
# [{'name': 'Robert Downey Jr.', 'role': 'Iron Man', 'nationality': 'the United States'}, 
# {'name': 'Chris Evans', 'role': 'Captain America', 'nationality': 'the United States'}]

 

 

 2) 람다 함수 사용 

friends = [{'name': 'Robert Downey Jr.', 'role': 'Iron Man', 'nationality': 'the United States'},
	    {'name': 'Tom Holland', 'role': 'Spider Man', 'nationality': 'the United Kingdom'}, 
            {'name': 'Chris Evans', 'role': 'Captain America', 'nationality': 'the United States'},
            {'name': 'Chris Hemsworth', 'role': 'Thor', 'nationality': 'Australia'}]
    
filter(lambda f: f["nationality"] != "the United States", friends) # <filter object at 0x000001E45BF7ADD8>

list(filter(lambda f: f["nationality"] != "the United States", friends)) 
# [{'name': 'Tom Holland', 'role': 'Spider Man', 'nationality': 'the United Kingdom'},
# {'name': 'Chris Hemsworth', 'role': 'Thor', 'nationality': 'Australia'}]

 

 

 Falsy Values 제거 

* 0, False, None, [], () ..

list1 = [0, 1, 2, 3, False, 0, 4]
list2 = [1, False, 2, 3, None, 'abc']
list3 = [[], (), 'a', 'b', 1, 2]

print(list(filter(bool, list1))) # [1, 2, 3, 4]
print(list(filter(bool, list2))) # [1, 2, 3, 'abc']
print(list(filter(bool, list3))) # ['a', 'b', 1, 2]

 

 

 

'Python' 카테고리의 다른 글

[Python] python split(문자열 분리하기)  (0) 2021.01.19
[Python] python set(집합)  (0) 2021.01.19

 

https://www.python.org/static/community_logos/python-logo-inkscape.svg

 

 문자열을 특정 기준으로 쉽게 분리 가능! 

* 결과는 리스트

 

 

 1) 공백 기준 

hbd = 'Happy Birthday'
print(hbd) # 'Happy Birthday'
hbd.split() # ['Happy' 'Birthday']

* hbd.split(' ')도 가능

 

 

 2) 문자 기준 

hbd = 'Happy Birthday'
print(hbd) # 'Happy Birthday'
hbd.split('a') # ['H', 'ppy Birthd', 'y']

 

 

 3) 문자열 기준 

hbd = 'Happy Birthday'
print(hbd) # 'Happy Birthday'
hbd.split('th') # ['Happy Bir', 'day']

 

'Python' 카테고리의 다른 글

[Python] python 내장 함수 filter  (0) 2021.01.19
[Python] python set(집합)  (0) 2021.01.19

 

https://www.python.org/static/community_logos/python-logo-inkscape.svg

 

 

 * set은 순서를 보장하지 않는다. 

 

 Set(집합)의 원소는 중복되지 않음! 

  → 중복 제거를 쉽게 할 수 있다.

set1 = set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
print(set1) # {1, 2, 3, 4}

 

 

 

 1) 원소 추가 (add) - 하나의 값만 추가 가능 

set1 = set([1, 2])
set1.add(3)
set1.add('abc')
set1.add(('a', 'b', 'c'))
print(set1) # {'abc', 1, 2, 3, ('a', 'b', 'c')}

 

 2) 여러 개의 원소 추가 (update) - [리스트]나 {set}으로 추가 

* 중복된 값이면 중복이 제거됨

set1 = {1, 2, 3}
set1.update([2])
print(set1) # {1, 2, 3}
set1.update([4, 5, 6])
print(set1) # {1, 2, 3, 4, 5, 6}
set1.update({7, 8})
print(set1) # {1, 2, 3, 4, 5, 6, 7, 8}
set1.update({3, 3, 3, 'a'})
print(set1) # {1, 2, 3, 4, 5, 6, 7, 8, 'a'}

 

 3) 원소 제거 (remove, discard) 

* 차이: set에 원소가 없는 경우 KeyError 발생 여부

set1 = {1, 2, 3}
set1.remove(2)
print(set1) # {1, 3}
set1.remove(2) # KeyError
set1.discard(2)
print(set1) # {1, 3}

 

 4) 연산자 (합집합 |, 교집합 &, 차집합 -, 대칭차집합 ^) 

* 대칭차집합 = 합집합 - 교집합

a = {1, 2, 3}
b = {3, 4, 5}
print(a|b) # {1, 2, 3, 4, 5}
print(a&b) # {3}
print(a-b) # {1, 2}
print(a^b) # {1, 2, 4, 5}

a.union(b), a.intersection(b), a.difference(b), a.symmetric_difference(b)로도 연산 가능!

 

 

 

참고: aigong.tistory.com/30

 

Python 내장 함수 : set 함수 사용하기 - 아이공

Python 내장 함수 : set 함수 사용하기 - 아이공 설명에 앞서 정제된 표현이 되어있는 아래 2개의 사이트를 방문하시는 것을 추천드립니다. 특히 1번이 아주 자세히 설명되어 있습니다. 최대한 친절

aigong.tistory.com

 

'Python' 카테고리의 다른 글

[Python] python 내장 함수 filter  (0) 2021.01.19
[Python] python split(문자열 분리하기)  (0) 2021.01.19

+ Recent posts