๐ŸŒCoding/๐ŸŒŸPython3 10

[Python] list, tuple -> String

๐Ÿ‘‡์—ฌ๊ธฐ์„œ๋„๐Ÿ‘‡ ํ•œ๋ฒˆ ๊ธฐ๋กํ–ˆ๋˜ ์ ์ด ์žˆ์—ˆ์ง€๋งŒ, ์ข…์ข… ๊นŒ๋จน๊ธฐ์— ๋‹ค์‹œ ์ž‘์„ฑํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค  https://melody-coding.tistory.com/46 [Python] listPython3์˜ list์— ๊ด€ํ•ด ๋„์ ๋„์ โœ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) list  โ‘  list ์„ ์–ธx=[] #list ์„ ์–ธ ๋ฆฌ์ŠคํŠธ ์„ ์–ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จ!! x=[i for i in range(5)] #[0, 1, 2, 3, 4]  ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•melody-coding.tistory.com tuple์—์„œ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์•ผ ํ•  ์ผ์ด ์ƒ๊ฒผ๋‹คlist์—์„œ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋˜ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์ด ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ ์•„๋‹Œ๊ฐ€? ๐Ÿ˜‰ list, tuple -> ๋ฌธ์ž์—ดtemp=['a','b','c'] #listtemp=('a','b','c') #..

[Python] heapq

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์ฒ˜์Œ์œผ๋กœ heapq๋ฅผ ์ ‘ํ–ˆ๋‹ค. ๐Ÿ˜‰ heapq ๋ž€? ์šฐ์„ ์ˆœ์œ„ ํ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ ๋„ ํ•˜๋Š” ํž™ ํ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„์„ ์ œ๊ณตํ•œ๋‹ค.ํž™์€ ๋ชจ๋“  ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ž์‹๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ–๋Š” ์ด์ง„ํŠธ๋ฆฌ์ด๋‹ค. ๐Ÿ˜‰ heapq importimport heapq ๐Ÿ˜‰ heapq ํ•จ์ˆ˜heap=[] ์›์†Œ ์ถ”๊ฐ€#์›์†Œ ์ถ”๊ฐ€heapq.heappush(heap, ์›์†Œ ๊ฐ’)heapq.heappush(heap,(-์›์†Œ ๊ฐ’, ์›์†Œ ๊ฐ’)) #์ตœ๋Œ€ ํž™ ๊ตฌํ˜„์›์†Œ ์ œ๊ฑฐ#์›์†Œ ์ œ๊ฑฐheapq.heappop(heap) #heap์—์„œ ๊ฐ€์žฅ ์ž‘์€ ํ•ญ๋ชฉ ํŒ

[Python] lambda

ํŒŒ์ด์ฌ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•ด๊ฒฐํ•  ๋•Œ ์ข…์ข… ๋ดค๋˜ ์นœ๊ตฌ์ด๋‹ค!๋ฌผ๋ก  ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค ๊ณ„์† ๊ฒ€์ƒ‰์„ ํ–ˆ์ง€๋งŒ...์ด์ œ๋ผ๋„ ์ •๋ฆฌํ•ด๋ณด๋ คํ•œ๋‹ค! ๐Ÿ˜‰ ๋žŒ๋‹ค (lambda) lambda ์ธ์ž: ํ‘œํ˜„์‹์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋ฌผ๋ก  ์ด๋ ‡๊ฒŒ๋งŒ ์จ๋†“๋Š”๋‹ค๋ฉด ๋ชจ๋ฅด๋Š” ๊ฒŒ ๋‹น์—ฐํ•˜๋‹ค! ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋„ ํ•จ๊ป˜ ์ ์–ด๋‘๊ฒ ๋‹ค!  numbers.sort(key=lambda x:x*4, reverse=True)  ๋‹ค์ค‘ ์กฐ๊ฑด# ๊ธฐ์ค€ ์•ž์— -๋ฅผ ๋ถ™์ด๋ฉด ๋‚ด๋ฆผ ์ฐจ์ˆœarr.sort(key=lambda x:(-int(x[1]), int(x[2]),-int(x[3]),x[0]))

[Python] ์ˆœ์—ด, ์กฐํ•ฉ

๐Ÿ˜‰ ์กฐํ•ฉ from itertools import combinations # ์กฐํ•ฉ(์ˆœ์„œx)nums=[1,2,3,4]arr = list(combinations(nums, 3)) #์ถœ๋ ฅ: [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]   ๐Ÿ˜‰ ์ˆœ์—ด from itertools import permutations # ์ˆœ์—ด(์ˆœ์„œ o)nums=[1,2,3,4]arr = list(permutations(nums, 3)) #์ถœ๋ ฅ: [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), # (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), # ..

[Python] ๋”•์…”๋„ˆ๋ฆฌ

๋”•์…”๋„ˆ๋ฆฌ ์“ธ ๋•Œ๋งˆ๋‹ค ๊นŒ๋จน..๐Ÿ˜ต ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋งŒ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋Œฑ  ๐Ÿ™ƒ๋”•์…”๋„ˆ๋ฆฌ - key: value ํ˜•์‹์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ   ๐Ÿ™ƒ๋”•์…”๋„ˆ๋ฆฌ ์„ ์–ธ dic = {}dic = dict()dic = {"Key":"Value"}   ๐Ÿ™ƒ๋”•์…”๋„ˆ๋ฆฌ ์ถ”๊ฐ€dic['์•„์ด๋””']='๋น„๋ฐ€๋ฒˆํ˜ธ'   ๐Ÿ™ƒ๋”•์…”๋„ˆ๋ฆฌ ์‚ญ์ œdel dic['์•„์ด๋””']   ๐Ÿ™ƒ๋”•์…”๋„ˆ๋ฆฌ Value ๊ฐ€์ ธ์˜ค๊ธฐprint(dic['์•„์ด๋””'])print(dic.get('์•„์ด๋””'))   ๐Ÿ™ƒ๋”•์…”๋„ˆ๋ฆฌ Key ์žˆ๋Š”์ง€ checkif '์•„์ด๋””' in dic : print("yes")   ๐Ÿ™ƒ๋”•์…”๋„ˆ๋ฆฌ value๋ฅผ ์ด์šฉํ•˜์—ฌ key ์ฐพ๊ธฐfor key,value in arr.items(): if value=='value ๊ฐ’': print(key)  ?..

[๊ฐ€์ƒํ™˜๊ฒฝ] ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์„ค์ •

๐Ÿ˜Š python ํ™˜๊ฒฝ ์„ค์ • ๋ฐ python ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ธฐ  ๐Ÿ™„ cmd 1๋‹จ๊ณ„) ํŒŒ์ด์ฌ ๋””๋ ‰ํ† ๋ฆฌ ๋งŒ๋“ค๊ธฐ: python -m venv ๋””๋ ‰ํ† ๋ฆฌ๋ช… 2๋‹จ๊ณ„) cd ํŒŒ์ด์ฌ ๋””๋ ‰ํ† ๋ฆฌ๋ช…\Scripts 3๋‹จ๊ณ„) ๊ฐ€์ƒํ™˜๊ฒฝ ์‹คํ–‰: activate         ๊ฐ€์ƒ ํ™˜๊ฒฝ ์ข…๋ฃŒ: deactivate.bat 4๋‹จ๊ณ„) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜: pip install pendulum                                pip install pytest         ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ชฉ๋ก: pip list๐Ÿ™„ vscode 5๋‹จ๊ณ„) vscode ์‹คํ–‰: code 6๋‹จ๊ณ„) EXTENSIONS์—์„œ python ์„ค์น˜ 7๋‹จ๊ณ„) View-Command Palette(ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์„ ํƒ)-๋””๋ ‰ํ† ๋ฆฌ\Scripts\pytho..

[Python] list

Python3์˜ list์— ๊ด€ํ•ด ๋„์ ๋„์ โœ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) list  โ‘  list ์„ ์–ธx=[] #list ์„ ์–ธ ๋ฆฌ์ŠคํŠธ ์„ ์–ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จ!! x=[i for i in range(5)] #[0, 1, 2, 3, 4]  ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.    โ‘ก list ์›์†Œ ์ถ”๊ฐ€x.append(5) #list ์›์†Œ ์ถ”๊ฐ€ append๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.  x.insert(1,"hi") #['a', 'hi', 'b', 'c', 'd'] insert(์›์†Œ ์ถ”๊ฐ€ํ•  index, ์ถ”๊ฐ€ํ•  ์›์†Œ ๊ฐ’)    โ‘ข list ์›์†Œ ์‚ญ์ œ  ๋ฆฌ์ŠคํŠธ ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹น  ex) x=["a", "b", "c"..

[Python] ์ž…์ถœ๋ ฅ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Python3์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์— ๊ด€ํ•ด ๋„์ ๋„์  โœ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค 1) ์ž…๋ ฅx=input()  ๋ณดํ†ต ์ œ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆœ input()์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.x=input().split() #๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅx,y=input().split() #๊ฐ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ ๋ฒˆ์— ๊ฐ’์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•ด split์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. split์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. x=list(map(int,input().split())) โ€ป map: ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ์ง€์ •๋œ ํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค ์ด ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€๋ฉฐ์ •์ˆ˜๋กœ ์ €์žฅํ•˜๊ณ  ์‹ถ์–ด ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ์•Œ๊ฒŒ ๋œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค...