python
-
Python Class - 5Programming/python 2021. 6. 29. 21:08
Python magic method 혹은 special method라고 불리는 것들이다. 우리는 이미 magic method나 special method를 알고 있다. 왜냐하면 우리가 자주 사용해왔기 때문이다. 결국, 파이썬 내부에 구현된 메소드들을 magic method 혹은 special method라 불린다. magic method의 종류를 보려면 dir 명령어를 이용하며 된다. __로 시작하는 method들이 magic method들이다. dir(int) dir(float)
-
Python Class - 4Programming/python 2021. 6. 29. 21:04
1) NamedTuple 일반적인 tuple을 사용할때는 [0], [1]으로 접근을 해야한다. # 일반적인 튜플 사용 pt1 = (1.0, 5.0) pt2 = (2.5, 1.5) from math import sqrt line_leng1 = sqrt((pt2[0] - pt1[0]) ** 2 + (pt2[1] - pt1[1]) ** 2) 하지만 이름으로 접근해야할때는 어떻게 해야될까? 좌표계 예제인데, [0],[1]이 아닌 x,y로 접근하고 싶다. 이럴때 collections의 namedtuple을 사용하면 된다. # 네임드 튜플 선언 Point = namedtuple('Point', 'x y') # 두 점 선언 pt1 = Point(1.0, 5.0) pt2 = Point(2.5, 1.5) # 계산 lin..
-
Python Class - 3Programming/python 2021. 6. 24. 22:19
왜 클래스의 메소드 앞에는 self가 붙을까? class Student(): # 클래스 변수 student_count = 0 def __init__(self, name, number): # 인스턴스 변수 self._name = name self._number = number Student.student_count += 1 def __str__(self): return 'str {}'.format(self._name) def __repr__(self): return 'repr {}'.format(self._name) def detail_info(self): print('current id = {}'.format(id(self))) print('Student detail info : {} {}'.format..
-
Python Class - 2Programming/python 2021. 6. 24. 22:17
클래스 변수 와 인스턴스 변수 Swift에도 존재하지만, 클래스 변수 : 클래스 전체에서 공유하는 변수 인스턴스 변수 : 생성된 객체 안에서만 공유되는 변수 # 클래스 변수와 인스턴스 변수 # 클래스 변수는 모든 클래스에서 공유, 인스턴스 변수는 생성후 사용 가능 class Student(): # 클래스 변수 student_count = 0 def __init__(self, name, number): # 인스턴스 변수 self._name = name self._number = number Student.student_count += 1 def __str__(self): return 'str {}'.format(self._name) def __repr__(self): return 'repr {}'.form..
-
Python Class - 1Programming/python 2021. 6. 24. 22:16
클래스 구조 : 재사용성 증가, 코드 반복 최소화, 메소드 활용 : magic method의 __str__, __repr__을 자주 활용하자 class Student(): def __init__(self, name, number): self._name = name self._number = number def __str__(self): return 'str : {}, {}'.format(self._name, self._number) def __repr__(self): return 'repr : {}, {}'.format(self._name, self._number) stu1 = Student('Kim', 1) print(stu1.__dict__) # {'_name': 'Kim', '_number': 1}..
-
Bisect ModuleProgramming/python 2021. 3. 9. 11:05
Bisect python에서 제공하는 패키지 이진 탐색을 쉽게 할 수 있도록 하는 함수 배열에서 특정 값을 찾는 방법은 1) for문을 앞에서부터 돌린다. 2) left, right를 둬서 이분 탐색을 한다. bisect는 2)번을 좀 더 쉽게 할 수 있도록 만들어 놓은 함수이다. 예를 들어서 점수에 따른 등급을 찾는다고 해보자 arr = [60, 70, 80, 90] val = ['F', 'D', 'C', 'B', 'A'] print(bisect.bisect_left(arr, 89),bisect.bisect_right(arr, 89), val[bisect.bisect_right(arr, 89)]) # 3 3 B print(bisect.bisect_left(arr, 79), bisect.bisect_ri..
-
링크드리스트Programming/Data Structure 2020. 10. 29. 08:39
연결리스트 1. 링크드리스트 구조 링크드리스트는 떨어진 곳을 존재하느 데이터를 포인터로 가르켜서 관리하는 구조 C언어에서는 포인터를 사용하여 다음 주소를 가르키도록 하지만, python에서는 객제지향을 가지고 구현 링크드리스트의 기본 용어 노드(Node) : 데이터의 기본 저장 단위(데이터값, 포인터)로 구성 포인터(Pointer) : 각 노드안에서 다음 Node와의 연결정보를 가지고 있음 2. Python으로 구현한 링크드 리스트 class Node: def __init__(self, data, next=None): self.data = data delf.next = next node1 = Node(1) node2 = Node(2) node1.next = node2 tNode = node1 while ..
-
StackProgramming/Data Structure 2020. 10. 19. 23:27
Stack에 대해서 알아보자 * 데이터를 제한적으로 접근할 수 있는 구조 * 한쪽끝에서만 데이터를 넣고 뺄수 있는 구조 * 가장 나중에 쌓은 데이터가 제일 먼저 나가는 구조 프로세스에서 많이 쓰임 1. 스택 구조 스택은 LIFO(Last In, First Out)구조 대표적인 함수 : push,(데이터 넣기), pop(데이터 빼기) Visualgo site에서 push, pop 기능으로 확인해보자 visualgo.net/en/list?slide=1 VisuAlgo - Linked List (Single, Doubly), Stack, Queue, Deque VisuAlgo is free of charge for Computer Science community on earth. If you like Visu..