파이썬 자료형 정리
# 컨테이너(Container : 서로다른 자료형[list, tuple, collections.deque],
# vs Flat : 한 개의 자료형[str,bytes,bytearray,array.array, memoryview])
[ 1, 'a', 'asd'] vs 'hello'
# 가변(list, bytearray, array.array, memoryview, deque)
# vs 불변(tuple, str, bytes)
list_a[0] = 33 가능함 그래서 가변
tuple_a[0] = 33 X 불변
strA = 'abcdefu' , strA[0] = A X 불변
그렇담 불변을 바꾸려면 어캅니까!?
불변의 data를 가변에 넣어주고 수정해서 써먹으면 됨
방법 예시 짧게
#ord는 아스키로 바꿔주는 거 chr은 아스키를 문자열로 바꿔주는거
#이렇게 불변 -> 가변
#이렇게 불변 -> 가변 해주면서 조건넣어서 수정도 가능
출력결과
코드
더보기
# Chapter04-01
# 파이썬 심화
# 시퀀스형
# 컨테이너(Container : 서로다른 자료형[list, tuple, collections.deque],
# Flat : 한 개의 자료형[str,bytes,bytearray,array.array, memoryview])
# 가변(list, bytearray, array.array, memoryview, deque)
# vs 불변(tuple, str, bytes)
# 리스트 및 튜플 고급
# 지능형 리스트(Comprehending Lists)
# Non Comprehending Lists
chars = '+_)(*&^%$#@!~)'
code_list1 = []
#이렇게 불변 -> 가변
for s in chars:
# 유니코드 리스트
code_list1.append(ord(s))
# Comprehending Lists
code_list2 = [ord(s) for s in chars]
#이렇게 불변 -> 가변 해주면서 조건넣어서 수정도 가능
# Comprehending Lists + Map, Filter
# 속도 약간 우세
code_list3 = [ord(s) for s in chars if ord(s) > 40]
code_list4 = list(filter(lambda x: x > 40, map(ord, chars)))
# 전체 출력
print(code_list1)
print(code_list2)
print(code_list3)
print(code_list4)
print([chr(s) for s in code_list1])
print([chr(s) for s in code_list2])
print([chr(s) for s in code_list3])
print([chr(s) for s in code_list4])
#ord는 아스키로 바꿔주는 거 chr은 아스키를 문자열로 바꿔주는거
print()
print()
'C++,python (인프런+사이트) > python 파이썬 정리' 카테고리의 다른 글
튜플 고급 문법 (자주 쓰이는거 위주로 정리) (0) | 2022.04.02 |
---|---|
Comprehending Lists으로 Generator 만들기 + 깊복,얕복 (0) | 2022.04.01 |
List Comprehension 리스트 컴프리헨션 +(딕셔너리 + 튜플) (0) | 2022.04.01 |
매직 메소드_네임드튜플 (0) | 2022.04.01 |
매직 메소드_오퍼레이터같은 (0) | 2022.03.31 |
댓글