print(dir({'1': 'abc'})) # 딕셔너리에서 가지고 있는 함수들 내역
"내장함수.py"
['__class__', '__class_getitem__', '__contains__','__delattr__',
'__delitem__','__dir__', '__doc__','__eq__', '__format__','__ge__',
'__getattribute__','__getitem__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__ior__', '__iter__', '__le__','__len__',
'__lt__', '__ne__', '__new__', '__or__', '__reduce__',
'__reduce_ex__', '__repr__','__reversed__','__ror__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear',
'copy','fromkeys','get', 'items', 'keys', 'pop', 'popitem',
'setdefault','update', 'values']
print(dir("python")) # 문자열에서 가지고 있는 함수들 내역
"내장함수복습.py"
['__add__', '__class__', '__contains__', '__delattr__','__dir__',
'__doc__','__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold',
'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii',
'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric',
'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix',
'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
def add(a):
return a * 3
li = [1, 2, 3]
print(list(map(add, li)))
print(tuple(map(add, li)))
>>> [3, 6, 9]
(3, 6, 9)
li2 = list(map(add, li))
print(li2)
>>> [3, 6, 9]
li = [1, 2, 3, 4, 5]
def filtering(a):
if a % 2 == 0:
return True
return False
print(list(filter(filtering, li)))
print(tuple(filter(filtering, li)))
>>> [2, 4]
(2, 4)
l2 = tuple(filter(filtering, li))
print(l2)
>>> (2, 4)
map이나 fliter를 쓰는이유 : 함수에 반복문을 추가해서 할수 있지만 코드의 단순화를 위해 내장함수를 사용한다.
a = [4, 5, 6]
b = ['d', 'e', 'f']
li = list(zip(a, b))
print(li)
>>> [(4, 'd'), (5, 'e'), (6, 'f')]
print(list(zip([1, 2, 3], [4, 5, 6])))
print(list(zip([1, 2, 3], ['a', 'b', 'c'])))
>>> [(1, 4), (2, 5), (3, 6)]
[(1, 'a'), (2, 'b'), (3, 'c')]