Coding/Python

[python] ์ž๋ฃŒ๊ตฌ์กฐ(๋ฆฌ์ŠคํŠธ, ์‚ฌ์ „, ํŠœํ”Œ, ์„ธํŠธ)

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 10. 20. 15:28

๋ฆฌ์ŠคํŠธ

subway1 = 10 

subway2 = 20

subway3 = 30 

 

์ด๊ฑฐ๋ฅผ

subway = [10, 20, 30] ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. 

 

ex) 

subway = ["์œ ์žฌ์„", "์กฐ์„ธํ˜ธ", "๋ฐ•๋ช…์ˆ˜"]

 

#์กฐ์„ธํ˜ธ์”จ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ์— ํƒ€๊ณ  ์žˆ๋Š”๊ฐ€? 

subway.index("์กฐ์„ธํ˜ธ")

 

#ํ•˜ํ•˜๊ฐ€ ๋‹ค์Œ ์ •๋ฅ˜์žฅ์—์„œ ๋‹ค์Œ ์นธ์— ํƒ‘์Šน

subway.append("ํ•˜ํ•˜") 

 

#์ •ํ˜•๋ˆ์„ ์œ ์žฌ์„/์กฐ์„ธํ˜ธ ์‚ฌ์ด์— ํƒœ์›€

subway.insert(1,"์ •ํ˜•๋ˆ")

 

#์ง€ํ•˜์ฒ ์— ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋’ค์—์„œ ํ•œ ๋ช…์”ฉ ํ•˜์ฐจ

subway.pop()

 

#๊ฐ™์€ ์ด๋ฆ„์˜ ์‚ฌ๋žŒ์ด ๋ช‡ ๋ช… ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

subway.count("์œ ์žฌ์„")

 

#์ •๋ ฌ

num_list = [1, 2, 3, 4, 5] 

num_list.sort()  #์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

num_list.reverse()  #๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

 

#๋ชจ๋‘ ์ง€์šฐ๊ณ  ์‹ถ์„ ๋•Œ

num_list.clear()

 

#๋ฆฌ์ŠคํŠธ๋Š” ์ž๋ฃŒํ˜•์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ์„ž์–ด์„œ ์“ธ ์ˆ˜ ์žˆ์Œ

mix_list = ["์กฐ์„ธํ˜ธ", 20, True]

 

#๋ฆฌ์ŠคํŠธ ํ™•์žฅ

num_list.extend(mix_list)

 

 

์‚ฌ์ „

(๋ชฉ์š•ํƒ• ์บ๋น„๋„ท ์˜ˆ์‹œ๋ฅผ ํ†ตํ•œ ์„ค๋ช…)

cabinet = {3:"์œ ์žฌ์„", 100:"๊น€ํƒœํ˜ธ",๋Œ€๊ด„ํ˜ธ ์‚ฌ์šฉ

cabinet[3]

cabient[100]

cabinet.get(3)

cabinet.get(5, "์‚ฌ์šฉ๊ฐ€๋Šฅ") 

#get์„ ์‚ฌ์šฉํ•˜๋ฉด ํ‚ค ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ none ๋ฐ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰, [ ]๋Š” ๊ฐ’ ์—†๋Š” ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ

 

#ํ•ด๋‹น ํ‚ค๊ฐ€ ์žˆ๋Š๋ƒ? 

3 in cabinet #True

5 in cabinet #False

 

#์ •์ˆ˜๊ฐ€ ์•„๋‹Œ str๋„ ๊ฐ€๋Šฅ

cabinet = {"A-3":"์œ ์žฌ์„", "B-100":"๊น€ํƒœํ˜ธ"} 

 

#์ƒˆ ์†๋‹˜

cabinet["C-20"] = "์กฐ์„ธํ˜ธ"

 

#์†๋‹˜์ด ๊ฐ„ ๊ฒฝ์šฐ

 del cabinet["A-3"] 

 

#key๋“ค๋งŒ ์ถœ๋ ฅ

cabinet.keys()

#values๋“ค๋งŒ ์ถœ๋ ฅ

cabinet.values()

 

#key, value ์Œ์œผ๋กœ ์ถœ๋ ฅ

cabinet.item()

 

#๋ชฉ์š•ํƒ• ํ์ 

cabinet.clear()

 


ํŠœํ”Œ

๋‚ด์šฉ ๋ณ€๊ฒฝ์ด๋‚˜ ์ถ”๊ฐ€๋ฅผ ํ•  ์ˆ˜ ์—†์Œ, but ์†๋„๊ฐ€ ๋น ๋ฆ„

๋”ฐ๋ผ์„œ, ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๊ฒƒ ์„ค์ •ํ•  ๋•Œ ์ข‹์Œ

 

menu = ("๋ˆ๊นŒ์Šค", "์น˜์ฆˆ๊นŒ์Šค")

menu[0]

menu[1]

 

name = "๊น€์ข…๊ตญ"

age = 20 

hobby = "์ฝ”๋”ฉ" 

์œ„ ๋‚ด์šฉ์„ ํŠœํ”Œ๋กœ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค

(name, age, hobby) = ("๊น€์ข…๊ตญ", 20, "์ฝ”๋”ฉ" )

 

 

์„ธํŠธ

์ค‘๋ณต X, ์ˆœ์„œ x

my_set = {1, 2, 3, 3, 3} 

print(my_set) #{1, 2, 3} 

 

java = {"์œ ์žฌ์„", "๊น€ํƒœํ˜ธ", "์–‘์„ธํ˜•"}

python = set(["์œ ์žฌ์„", "๋ฐ•๋ช…์ˆ˜"])

 

#๊ต์ง‘ํ•ฉ

java & python 

java.intersection(python)

 

#ํ•ฉ์ง‘ํ•ฉ

java | python 

java.union(python) 

 

#์ฐจ์ง‘ํ•ฉ

java - python 

java.difference(python) 

 

#python ํ•  ์ค„ ์•„๋Š” ์‚ฌ๋žŒ ์ฆ๊ฐ€ 

python.add("๊น€ํƒœํ˜ธ")

 

#java ๊นŒ๋จน์Œ

java.remove("๊น€ํƒœํ˜ธ")

 

 

์ž๋ฃŒ ๊ตฌ์กฐ์˜ ๋ณ€๊ฒฝ

menu = {"์ปคํ”ผ", "์šฐ์œ ", "์ฃผ์Šค"} #์„ธํŠธํ˜•

menu = list(menu) #๋ฆฌ์ŠคํŠธํ˜• ๋ณ€๊ฒฝ

menu = tuple(menu) #ํŠœํ”Œํ˜• ๋ณ€๊ฒฝ 

 

 

set ์ผ๋•Œ : { }

list ์ผ๋•Œ : [ ]

tuple ์ผ๋•Œ : ( )

 

ํ€ด์ฆˆ

ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋Œ€ํšŒ์—์„œ ๋Œ“๊ธ€ ์ด๋ฒคํŠธ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
๋Œ“๊ธ€ ์ž‘์„ฑ์ž ์ค‘์—์„œ ์ถ”์ฒจ์„ ํ†ตํ•ด 1๋ช…์€ ์น˜ํ‚จ, 3๋ช…์€ ์ปคํ”ผ ์ฟ ํฐ์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

์กฐ๊ฑด1: ํŽธ์˜์ƒ ๋Œ“๊ธ€์€ 20๋ช…์ด ์ž‘์„ฑํ•˜์˜€๊ณ , ์•„์ด๋””๋Š” 1~20์ด๋ผ๊ณ  ๊ฐ€์ •
์กฐ๊ฑด2: ๋Œ“๊ธ€ ๋‚ด์šฉ๊ณผ ์ƒ๊ด€์—†์ด ๋ฌด์ž‘์œ„๋กœ ์ถ”์ฒจ, ์ค‘๋ณต์€ ๋ถ€๋ฝ
์กฐ๊ฑด3: random ๋ชจ๋“ˆ์˜ shuffle๊ณผ sample์„ ํ™œ์šฉ

์ถœ๋ ฅ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
-- ๋‹น์ฒจ์ž ๋ฐœํ‘œ --
์น˜ํ‚จ ๋‹น์ฒจ์ž :
์ปคํ”ผ ๋‹น์ฒจ์ž :
-- ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค --

 

๋‚ด๊ฐ€ ํ‘ผ ๊ฒƒ : 

๋”๋ณด๊ธฐ
from random import *
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
shuffle(lst)
chiken_winner = sample(lst, 1)
coffee_winner = sample(set(lst) - set(chiken_winner),3)

print("""-- ๋‹น์ฒจ์ž ๋ฐœํ‘œ --
์น˜ํ‚จ ๋‹น์ฒจ์ž : """ + str(chiken_winner) +
"""\n์ปคํ”ผ ๋‹น์ฒจ์ž : """+ str(coffee_winner) +
"""\n-- ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค --""")

 

ํ•ด์„ค: 

๋”๋ณด๊ธฐ
from random import *
users = range(1, 21) #1๋ถ€ํ„ฐ 20๊นŒ์ง€ ์ˆซ์ž๋ฅผ ์ƒ์„ฑ
users = list(users) #ํƒ€์ž…์„ range์—์„œ list๋กœ ๋ฐ”๊ฟˆ
shuffle(users)

winners = sample(users, 4) #4๋ช… ์ค‘ ํ•œ ๋ช…์€ ์น˜ํ‚จ, ์„ธ ๋ช…์€ ์ปคํ”ผ
print(" -- ๋‹น์ฒจ์ž ๋ฐœํ‘œ --")
print(" ์น˜ํ‚จ ๋‹น์ฒจ์ž : {0}".format(winners[0]))
print(" ์ปคํ”ผ ๋‹น์ฒจ์ž : {0}".format(winners[1:]))
print(" -- ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค --")