๐ Pythonic Code
- ํ์ด์ฌ ์คํ์ผ์ ์ฝ๋ฉ ๊ธฐ๋ฒ
- ํ์ด์ฌ ํน์ ์ ๋ฌธ๋ฒ์ ํ์ฉํ ํจ์จ์ ์ธ ์ฝ๋ฉ
- ๊ณ ๊ธ ์ฝ๋๋ฅผ ์์ฑํ ์๋ก ๋ ๋ง์ด ํ์
๐ Why Pythonic Code?
- ํ์ธ์ ์ฝ๋์ ๋ํ ์ดํด๋
- ๋ง์ ๊ฐ๋ฐ์๋ค์ด python ์คํ์ผ๋ก ์ฝ๋ฉํ๋ค.
- ํจ์จ์ฑ
- ๋จ์ํ for loop append๋ณด๋ค list๊ฐ ์กฐ๊ธ ๋ ๋น ๋ฅด๋ค.
- ๊ฐ์ง..?
- ์ ์ง ๋ชจ๋ฅผ ๊ณ ์์ ๋๋?
๐ Split()
# Split & Join
items = 'zero one two three'.split()#๋น์นธ์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ๋๋๊ธฐ
items
# ['zero', 'one', 'two', 'three']
example = 'python,jquery,javascript'#","์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ๋๋๊ธฐ
example.split(",")
example
# 'python,jquery,javascript'
a, b, c = example.split(",")#๋ฆฌ์คํธ์ ์๋ ๊ฐ ๊ฐ์ a,b,c ๋ณ์๋ก unpacking
print(a, b, c)
# python jquery javascript
example = "cs50.gachon.edu"
subdomain, domain, tld = example.split(".")
#"."์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ๋๋๊ธฐ -> unpacking
print(subdomain, domain, tld)
# cs50 gachon edu
๐ join()
String List๋ฅผ ํฉ์ณ ํ๋์ String์ผ๋ก ๋ฐํํ ๋ ์ฌ์ฉ.
colors = ['red', 'blue', 'green', 'yellow']
res = ''.join(colors)
res
# 'redbluegreenyellow'
res = ' '.join(colors)
res
# 'red blue green yellow'
res = ', '.join(colors)
res
# 'red, blue, green, yellow'
res = '-'.join(colors)
res
# 'red-blue-green-yellow'
๐ List Comprehension
- ๊ธฐ์กด list ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ๋ค๋ฅธ list๋ฅผ ๋ง๋๋ ๊ธฐ๋ฒ
- ํฌ๊ด์ ์ธ list, ํฌํจ๋๋ ๋ฆฌ์คํธ๋ผ๋ ์๋ฏธ๋ก ์ฌ์ฉ๋จ
- for + append๋ณด๋ค ์๋๊ฐ ๋น ๋ฅด๋ค.
res = []
for i in range(10):
res.append(i)
res
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
res = [i for i in range(10)]
res
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
res = [i for i in range(10) if i%2==0]
res
# [0, 2, 4, 6, 8]
word_1 = "Hello"
word_2 = "World"
res = [i+j for i in word_1 for j in word_2]
res
# โผ Result
['HW',
'Ho',
'Hr',
'Hl',
'Hd',
'eW',
'eo',
'er',
'el',
'ed',
'lW',
'lo',
'lr',
'll',
'ld',
'lW',
'lo',
'lr',
'll',
'ld',
'oW',
'oo',
'or',
'ol',
'od']
case_1 = ["A", "B", "C"]
case_2 = ["D", "E", "A"]
res = [i+j for i in case_1 for j in case_2]
res
# ['AD', 'AE', 'AA', 'BD', 'BE', 'BA', 'CD', 'CE', 'CA']
res = [i+j for i in case_1 for j in case_2 if not(i==j)]
# Filter : i์ j๊ฐ ๊ฐ๋ค๋ฉด list์ ์ถ๊ฐํ์ง ์์
res.sort()
res
# ['AD', 'AE', 'BA', 'BD', 'BE', 'CA', 'CD', 'CE']
words = "The quick brown fox jumps over the lazy dog".split()
words
# ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
stuff = [[w.upper(), w.lower(), len(w)] for w in words]
stuff
# Result โผ
[['THE', 'the', 3],
['QUICK', 'quick', 5],
['BROWN', 'brown', 5],
['FOX', 'fox', 3],
['JUMPS', 'jumps', 5],
['OVER', 'over', 4],
['THE', 'the', 3],
['LAZY', 'lazy', 4],
['DOG', 'dog', 3]]
๐ Enumerate
List์ element๋ฅผ ์ถ์ถํ ๋ ๋ฒํธ๋ฅผ ๋ถ์ฌ์ ์ถ์ถํจ.
# Enumerate
for i, v in enumerate(['tic', 'tac', 'toe']):
print(i, v)
# 0 tic
# 1 tac
# 2 toe
mylist = ["a", "b", "c", "d"]
list(enumerate(mylist))
# [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
{i:j for i, j in enumerate('Gachon University is an academic institute located in South Korea.'.split())}
# Result โผ
{0: 'Gachon',
1: 'University',
2: 'is',
3: 'an',
4: 'academic',
5: 'institute',
6: 'located',
7: 'in',
8: 'South',
9: 'Korea.'}
๐ Zip()
2๊ฐ์ list์ ๊ฐ์ ๋ณ๋ ฌ์ ์ผ๋ก ์ถ์ถํจ.
alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']
for a, b in zip(alist, blist):
print(a, b)
# a1 b1
# a2 b2
# a3 b3
a,b,c = zip((1,2,3), (10,20,30), (100,200,300))
print(a, b, c)
# (1, 10, 100) (2, 20, 200) (3, 30, 300)
[sum(x) for x in zip((1,2,3), (10,20,30), (100,200,300))]
# [111, 222, 333]
alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']
for i, (a, b) in enumerate(zip(alist, blist)):
print(i, a, b)
# 0 a1 b1
# 1 a2 b2
# 2 a3 b3
'๊ฐ๋ฐ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Advanced Python #5 [Notion : 1/4] (0) | 2023.04.30 |
---|---|
Advanced Python #4 [Notion : 1/3] (0) | 2023.04.30 |
Advanced Python #3 [Notion : 1/3] (0) | 2023.04.30 |
Advanced Python #1 [Notion : 1/2] (0) | 2023.04.30 |