๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ๋ฐœ/Python

Advanced Python #2 [Notion : 1/3]

๐Ÿ‘‰ 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