01.Pythonベース-組み込み関数
🌈 組み込み関数のクリーンアップ
🔥 abs(number)
🔥 divmod(num1,num2)
🔥 dir(object)
🔥 eval(string)
🔥 enumerate(iterable)
🔥 all(iterable)
🔥 any(iterable)
🔥 sorted(iterable)
🔥 zip(iterable)
🔥 filter(fn, iterable)
🔥 map(fn, iterable)
🔥 isinstance(object, class)
🔥 chr(i)
🔥 hex(int)
1. abs(number)
✍🏻 python
num = -7
print(abs(num)) # 7
2. divmod(num1,num2)
✍🏻 python
print(divmod(7, 3)) # (2, 1)
print(divmod(10, 2)) # (5, 0)
print(divmod(87, 7)) # (12, 3)
3. dir(object)
✍🏻 python
print(dir([1, 2, 3])) #['append', 'count', 'extend', 'index', 'insert', 'pop',...]
print(dir({'1':'a'})) # ['clear', 'copy', 'get', 'has_key', 'items', 'keys',...]
4. eval(string)
print(eval('1+2')) # 3
print(eval("'hi' + 'a'")) # 'hia'
print(eval('divmod(4, 3)')) # (1, 1)
5. enumerate(iterable)
✍🏻 python
# 리스트에서 활용
for i, name in enumerate(['body', 'foo', 'bar']):
print(i, name)
# 0 body
# 1 foo
# 2 bar
# 딕셔너리에서 활용
dict_enumerate = {"name":"jaewon", "age":"10", "location":"seoul", "birth":"03-03"}
for i, k in enumerate(dict_enumerate):
print(i, k)
# 0 name
# 1 age
# 2 location
# 3 birth
6. all(iterable)
✍🏻 python
a = [1,2,3,4,5]
b = [1,2,0,4,5]
c = [False, True, 1, 2, 3]
d = [True, 1, "", 2]
e = [1, 2, "a", True, "false"]
print(all(a)) # True
print(all(b)) # False
print(all(c)) # False
print(all(d)) # False
print(all(e)) # True
7. any(iterable)
入力パラメータは
✍🏻 python
a = [1,2,3,4,5]
b = [1,2,0,4,5]
c = [False, True, 1, 2, 3]
d = [True, 1, "", 2]
e = [1, 2, "a", True, "false"]
print(any(a)) # True
print(any(b)) # True
print(any(c)) # True
print(any(d)) # True
print(any(e)) # True
8. sorted(iterable)
iterable_list = [5,4,3,2,1]
iterable_tuple = ("z","c","a","p")
iterable_dict = {"d":1 , "b":20 , "u":31 , "k":42}
iterable_str = "4321"
print(sorted(iterable_list)) # [1, 2, 3, 4, 5]
print(sorted(iterable_tuple)) # ['a', 'c', 'p', 'z']
print(sorted(iterable_dict)) # ['b', 'd', 'k', 'u']
print(sorted(iterable_str)) # ['1', '2', '3', '4']
print(iterable_list) # [5, 4, 3, 2, 1]
print(iterable_tuple) # ('z', 'c', 'a', 'p')
print(iterable_dict) # {'d': 1, 'b': 20, 'u': 31, 'k': 42}
print(iterable_str) # 4321
li = [300,99,1,9,4,10]
print(li.sort()) # None
print(li) # [1, 4, 9, 10, 99, 300] -> list 자체를 바꿔버림
9. zip(iterable)
✍🏻 python
print(list(zip([1, 2, 3], [4, 5, 6]))) # [(1, 4), (2, 5), (3, 6)]
print(list(zip([1, 2, 3], [4, 5, 6], [7, 8, 9]))) # [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
print(list(zip("abc", "def"))) # [('a', 'd'), ('b', 'e'), ('c', 'f')]
10. filter(fn, iterable)
✍🏻 python
# 일반 함수를 이용했을 때,,
# 0보다 큰 숫자만 리턴해주는 함수 기능
def positive(l):
result = []
for i in l:
if i > 0:
result.append(i)
return result
print(positive([1,-3,2,0,-5,6])) # [1,2,6]
# 내장함수 filter를 이용했을 때,,
# 0보다 큰 숫자만 리턴해주는 함수 기능
def positive(x):
return x > 0
print(list(filter(positive, [1, -3, 2, 0, -5, 6]))) # [1,2,6]
# filter와 lambda 조합 활용 예시,,
# 0보다 큰 숫자만 리턴해주는 함수 기능
list(filter(lambda x: x > 0, [1, -3, 2, 0, -5, 6])) # [1,2,6]
11. map(fn, iterable)
✍🏻 python
# 함수로 표현 했을 때,
def two_times(numberList):
result = []
for number in numberList:
result.append(number*2)
return result
result = two_times([1, 2, 3, 4])
print(result) # [2, 4, 6, 8]
# 내장함수 map을 사용했을 때,
def two_times(num):
return num * 2
print(list(map(two_times, [1, 2, 3, 4]))) # [2, 4, 6, 8]
# map과 lambda 조합 활용 예시,,
print(list(map(lambda a: num*2, [1, 2, 3, 4]))) # [2, 4, 6, 8]
12. isinstance(object, class)
✍🏻 python
class Person: pass # 클래스 선언
a = Person() # a는 객체이자 Person()의 인스턴스
print(isinstance(a, Person)) #True
13. chr(i)
関数
✍🏻 python
print(chr(97)) #'a'
print(chr(48)) #'0'
14. hex(int)
✍🏻 python
print(hex(234)) # '0xea'
print(hex(3)) # '0x3'
Reference
この問題について(01.Pythonベース-組み込み関数), 我々は、より多くの情報をここで見つけました https://velog.io/@jewon119/01.Python-기초-내장-함수テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol