일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- DWG
- LeetCode
- concurrency
- Protocol
- shiba
- Generator
- Python Code
- Python
- 30. Substring with Concatenation of All Words
- 파이썬
- Decorator
- iterator
- 컴퓨터의 구조
- 프로그래머스
- Substring with Concatenation of All Words
- 시바견
- Python Implementation
- t1
- 109. Convert Sorted List to Binary Search Tree
- 밴픽
- 운영체제
- Regular Expression
- kaggle
- attribute
- Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- Class
- 715. Range Module
- data science
- 315. Count of Smaller Numbers After Self
- Today
- Total
목록Computer Science/Python (32)
Scribbling
Immutable dictionary: MappingProxyType Immutable Set: Frozenset from types import MappingProxyType d = {'a': 'apple', 'b': 'banana', 'c': 'carrot'} d_fixed = MappingProxyType(d) print(d_fixed['a']) s = frozenset([1, 2, 3]) print(s)
1. List / Generator Comprehension Using list / generator comprehension makes code more readable while enhancing efficiency. suits = ['Spades', 'Diamonds', 'Hearts', 'Clubs'] ranks = [str(i) for i in range(2, 11)] + list('JQKA') for card in (f"{suit} {rank}" for suit in suits for rank in ranks): print(card) 2. Tuple Unpacking a, b, *rest = range(1, 5) print(a, b, rest) 3. NamedTuple Namedtuple is..
In this post, I am dealing with 'magic methods' to create more pythonic data models. First Example: Card Deck Card = namedtuple('Card', ['rank', 'suit']) class FrenchDeck: ranks = [str(n) for n in range(2, 11)] + list('JQKA') suits = ['Spades', 'Diamonds', 'Hearts', 'Clubs'] def __init__(self): self._cards = [Card(rank, suit) for suit in FrenchDeck.suits for rank in FrenchDeck.ranks] def __len__..
1. Use deep copy list function copy() can be troublesome when the list has mutable objects. from copy import deepcopy l1 = [1, 2, [3, 4]] l2 = l1.copy() l3 = deepcopy(l1) l1[1] = 3 l1[2][0] = 4 print(l2) print(l3) 2. while--else for--else else part is executed when the loop is stopped by 'break' or when the loop wasn't executed. i = 0 while i > 10: print(i) else: print('no') for x in range(10): ..
Refer to the website below for the detailed instructions. https://www.w3schools.com/python/python_ref_string.asp Python String Methods W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more. www.w3schools.com 1. len(str) - returns the length of the string ..
Each of programming language has their own way of 'Integer Representation'. In most languages, integers are represented with 32 bits and the most significant bit (the leftmost bit) is for the sign of it. As a result, usual '32-bit int representation' has range of [-2**31+1, 2**31]. * 2**31 (INT_MAX) is 0xFFFFFFFF * -2*31+1 (-INT_MAX) is 0x80000000 (by 2's complement) However, python has somewhat..
정규 표현식 혹은 정규식은 문자열 매칭에 매우 유용하다. 정규 표현식에 대해 자세히 알고 싶다면 아래 링크를 참고하라. https://wikidocs.net/1642 07-1 정규 표현식 살펴보기 정규 표현식(Regular Expressions)은 복잡한 문자열을 처리할 때 사용하는 기법으로, 파이썬만의 고유 문법이 아니라 문자열을 처리하는 모든 곳에서 사용한다. 정 ... wikidocs.net https://dojang.io/mod/page/view.php?id=2435 파이썬 코딩 도장: 43.1 문자열 판단하기 Unit 43. 정규표현식 사용하기 정규표현식(regular expression)은 일정한 규칙(패턴)을 가진 문자열을 표현하는 방법입니다. 복잡한 문자열 속에서 특정한 규칙으로 된 문자열..
Decorator는 이미 만든 함수를 수정하지 않고, 함수 주변을 감싸는 방식으로 함수에 추가 기능을 구현한다. def trace(func): # wrapper는 아래처럼 가변 인수로 만들 수 있다. # 가변 인수가 아닌 경우, 원래 함수의 parameter 형태와 일치해야 한다. def wrapper(*args, **kwargs): ret = func(*args, **kwargs) print('{0}(args={1}, kwargs={2}) -> {3}'.format(func.__name__, args, kwargs, ret)) # 원래 함수가 return이 필요한 경우에는 wrapper도 return이 필요하다. return ret return wrapper @trace def get_max(*args..
1. Iterator __iter__, __next__를 가진 객체를 iterator protocol을 지원한다고 일컫는다. class Counter: def __init__(self, limit): self.num = 0 self.limit = limit def __iter__(self): return self def __next__(self): if self.num < self.limit: ret = self.num self.num += 1 return ret else: raise StopIteration for i in Counter(10): print(i, end=' ') 클래스에서 __getitem__ 메서드만 구현해도 이터레이터가 된다. class Counter: def __init__(sel..
Python Class 관련하여 잘 몰랐던 부분을 정리해둔다. 1. 비공개 속성 및 메서드 속성이나 메서드에 "__"를 붙이면 클래스 외부에서 접근할 수 없다. class person: def __init__(self, name, nickname): self.name = name self.__nickname = nickname def __tease(self): print(self.__nickname) 2. 클래스 속성 클래스 속성은 아래처럼 선언가능하며, 클래스명으로 접근하는 것이 가독성에 좋다. class Person: all_names = [] def __init__(self, name): Person.all_names.append(name) self.name = name 3. 정적 메서드 (Stat..