일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- t1
- concurrency
- LeetCode
- 715. Range Module
- DWG
- Substring with Concatenation of All Words
- 컴퓨터의 구조
- 43. Multiply Strings
- Python Implementation
- kaggle
- shiba
- Regular Expression
- Protocol
- 밴픽
- Class
- attribute
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- data science
- Python Code
- 315. Count of Smaller Numbers After Self
- Python
- 30. Substring with Concatenation of All Words
- Decorator
- Generator
- iterator
- 프로그래머스
- 시바견
- 운영체제
- 파이썬
Archives
- Today
- Total
Scribbling
Python Data Structures: Sequences 본문
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 very useful to use tuples as records.
from collections import namedtuple
Person = namedtuple('Person', 'name age')
p1 = Person('Morgan', 27)
print(p1.name)
print(p1[-1])
from collections import namedtuple
Person = namedtuple('Person', 'name age')
print(Person._fields)
info = ('JY', 27)
p2 = Person._make(info)
for k, v in p2._asdict().items():
print(k, v)
4. Slice object
In python, a:b:c denotes a slice(a, b, c) object.
invoice = '''
No...Price...Quantity...
01 $7.50 3
02 $8.49 4
03 $1.22 1000
'''
NO = slice(0, 5)
Price = slice(5, 13)
Quantity = slice(13, 20)
line_items = invoice.split('\n')[2:]
for item in line_items:
print(item[NO], item[Price], item[Quantity])
5. Byte object
There are 3 ways to create an byte object as below.
print(bytes(10))
print(bytes([10, 20, 30]))
print(b'hello world')
x = 'hello word'.encode()
print(x)
print(x.decode('utf-8'))
'Computer Science > Python' 카테고리의 다른 글
Python: Memoryview function (0) | 2022.03.23 |
---|---|
Python Immutable Dictionary, Set (0) | 2022.03.22 |
Python Magic Methods: Pythonic Data Model (0) | 2022.03.16 |
Python Grammar: Things that I forget often (0) | 2022.03.14 |
Python String Methods (0) | 2022.01.18 |