주의사항
- 아래 정리 내용은 모든 내장 함수 활용에 대한 모든 내용을 정리해둔 것이 아니라, 자주 사용하는 case에 대해서만 정리함
enumerate() : idx, value 모두 가져오고싶을때
>>> num_list = [1, 2, 3]
>>> for idx, value in enumerate(num_list):
... print(idx, value)
...
... 0 1
1 2
2 3
zip() : 두 리스트에서 같은 idx에 있는 element들을 묶어주고 싶을때
>>> numbers = [1, 2, 3]
>>> letters = ["A", "B", "C"]
>>> for pair in zip(numbers, letters):
... print(pair)
...
(1, 'A')
(2, 'B')
(3, 'C')
map(func, list) : func => list에 적용하고 싶을때
list(map(str,[3,3,3,3])))
=> ['3','3','3','3']
- element가 int로 구성된 list에
map()
함수 적용하여 element가 string으로 구성된 list로 변환해준다.
- map()만 한 상태에서 print하면 주소값만 나오기 때문에 list()를 반드시 씌워줘야한다
map(str,[3,3,3,3]))
=> <map object at 0x7fcc38051a10>
list(map(str,[3,3,3,3])))
=> ['3','3','3','3']
zip() & map()
- 예시1
- map을 활용해서 int로 구성된 list에 map() 함수 적용하여 string으로 구성된 list로 변환해준다.
- zip()을 이용해서 부호를 붙여준다
>>> for z in zip(['+', '-', '+', '-'], map(str,[3,3,3,3])):
... print(z)
... ('+', '3')
... ('-', '3')
... ('+', '3')
... ('-', '3')
- 예시2
- map을 활용해서 int로 구성된 list에 map() 함수 적용하여 string으로 구성된 list 만든다.
- zip()을 이용해서 부호를 붙여준다
- container라는 리스트에 다시 int로 변환해서 넣어준다
>>> container = []
>>> for z in zip(['+', '-', '+', '-'], map(str,[3,3,3,3])):
... container.append(int(z[0] + z[1]))
... print(container)
...
... [3, -3, 3, -3]
텍스트.split('구분자') : 구분자를 기준으로 text를 나눠주고 싶을때
>>> text1 = 'a-b-c'
>>> text2 = 'a b c'
>>>
>>> text_split1 = text1.split('-')
>>> text_split2 = text2.split(' ')
>>>
>>> print(text_split1)
... ['a', 'b', 'c']
>>> print(text_split2)
... ['a', 'b', 'c']
.join(리스트) : 구분자를 기준으로 list/tuple내 text를 합쳐주고 싶을때
>>> text_ls = ['a', 'b', 'c']
>>> text_tuple = ('a', 'b', 'c')
>>>
>>> print(''.join(text_ls))
... abc
>>> print('-'.join(text_tuple))
... a-b-c