Programming(74)
-
Python: 원소의 중복 제거
중복 제거 Python에서 중복을 제거하는 방법은 매우 간단하다. 단순하게 list를 set으로 변환하면 된다. alist = list('abcba') # ['a', 'b', 'c', 'b', 'a'] 위 list를 set으로 변환한다. aset = set(alist) # {'c', 'b', 'a'} auniq = list(aset) # ['c', 'b', 'a'] 그런데, 위 aset이 지니고 있는 각 원소들의 순서를 원본 list 내 원소들의 순서와 동일하게 하고 싶다면? 이 때는 다음과 같이 list 초기화 exppression을..
2021.12.15 -
Python instance check
Like any other programminmg languages, Python also provides a way to check what the type of the instance is. Simple, by using 'isinstance' or 'type' function, it is possible to check instance's type. the following code is about using 'isinstance and type' val = 7 ovals = [1, 2] vals = [] if isinstance(val, int): vals += val, if type(ovals) == list: vals += ovals
2021.12.13 -
[Python] Lambda expression (람다 표현식)
lambda param : body >>> add_10 = lambda x : x + 10 >>> >>> add_10(1) 11 >>> (lambda arg : arg + 10)(1) 11 >>> (lambda : 1)() 1 Python lambda expression 내에서는 변수 선인이 불가능 >>> y = 10 >>> (lambda arg : arg + y)(1) 11 >>> def plus(x): ... return x + 10 >>> list(map(plus, [1])) [11] >>> >>> list(map(plus, [1, 2])) [11, 12] >>> list(map(lambda arg : arg + 10, [1, 2])) [11, 12] map, filter, reduce 함수 사용 ..
2021.12.13 -
Python access member as string
You from time to time meet a case that you need to set member varialbe. In such a case, if you get a class with lots of member varialbe, then code to set/get is somewhat burden. In such case, there are very simple way to do. I'll let you know in this article. class definition Assume that there is a class with three member attributes as follow. class Item: def __init__(self): self.key = None ..
2021.12.10 -
Python access member with string
Python class의 맴버에 값을 설정해야 할 때가 있다. 그런데, member가 너무 많은 경우 일일이 값들을 set, get 하는 코드를 작성하기 힘들 수 있습니다. 이런 경우 매우 손쉽게 string 배열을 통해 값을 설정하는 법을 알려드리겠습니다. class 정의 아래와 같이 3개의 member attribute를 지니고 있는 class가 있다고 합시다. class Item: def __init__(self): self.key = None self.cat = None self.file = None 일반적인 값 설정 방법 위 class에 값을 설정하는 일반적인 방법은 다음과 같습니다. item = Item() item.key = 1 item.cat = 3 item.file = 'filen..
2021.12.10 -
openpyxl sheet
openpyxl import 가장 먼저 openpyxl을 import 해야 합니다. import openpyxl from openpyxl.styles.borders import Border, Sidedeclare workbook 작업할 Workbook instance를 선언합니다. wb = openpyxl.Workbook()create a new sheet workbook 내에 sheet을 생성합니다. wb.create_sheet('sheet 1', 0)sheet에 cell들을 추가하기 위해서 객체를 얻어옵니다. sheet = wb.get_sheet_by_name('sheet 1')adding cells use Border for each cell 각 cell에 적용할 bor..
2021.12.10