phone_book = { } # °ø¹é µñ¼Å³Ê¸®¸¦ »ý¼º
phone_book["È«±æµ¿"] = "010-1234-5678"
print(phone_book)
phone_book = {"È«±æµ¿": "010-1234-5678"}
phone_book["°°¨Âù"] = "010-1234-5679"
phone_book["À̼ø½Å"] = "010-1234-5680"
print(phone_book)
person_dic = {'Name': 'È«±æµ¿', 'Age': 7, 'Class': 'ÃʱÞ'}
print(person_dic['Name'])
print(person_dic['Age'])
print(phone_book["°°¨Âù"])
phone_book.keys()
phone_book.values()
phone_book.items()
for name, phone_num in phone_book.items():
print(name,':', phone_num)
phone_book = {"È«±æµ¿": "010-1234-5678"}
phone_book["°°¨Âù"] = "010-1234-5679"
phone_book["À̼ø½Å"] = "010-1234-5680"
for key in phone_book.keys():
print(key, ':', phone_book[key])
sorted(phone_book) # µñ¼Å³Ê¸®¸¦ Å°¸¦ ±âÁØÀ¸·Î Á¤·ÄÇÏ¸ç ¸®½ºÆ®¸¦ ¹Ýȯ
sorted_phone_book = sorted(phone_book.items(), key=lambda x: x[0])
print(sorted_phone_book)
del phone_book["È«±æµ¿"] # "È«±æµ¿" Å°¸¦ ÀÌ¿ëÇÏ¿© µñ¼Å³Ê¸®ÀÇ ÇÑ Ç׸ñ »èÁ¦
print(phone_book)
phone_book.clear()
print(phone_book)
items = { "Ä¿ÇÇÀ½·á": 7, "Ææ": 3, "Á¾ÀÌÄÅ": 2,
"¿ìÀ¯": 1, "Äݶó": 4, "Ã¥": 5 }
item = input("¹°°ÇÀÇ À̸§À» ÀÔ·ÂÇϽÿÀ: ");
print(items[item])
print("»çÀü ÇÁ·Î±×·¥ ½ÃÀÛ... Á¾·á´Â q¸¦ ÀÔ·Â")
dictionary = {}
while True:
st = input('$ ')
command = st[0] # ù ÀÔ·Â ¹®ÀÚ¸¦ ÃßÃâÇÑ´Ù
if command == '<':
st = st[1:]
inputStr = st.split(':')
if len(inputStr) < 2 :
print('ÀÔ·Â ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù.')
else:
dictionary[inputStr[0].strip()] = inputStr[1].strip()
elif command == '>':
st = st[1:]
inputStr = st.strip()
if inputStr in dictionary:
print(dictionary[inputStr])
else :
print('{}°¡ »çÀü¿¡ ¾ø½À´Ï´Ù.'.format(inputStr))
elif command == 'q':
break
else :
print('ÀÔ·Â ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù.')
print("»çÀü ÇÁ·Î±×·¥À» Á¾·áÇÕ´Ï´Ù.")
numbers = {2, 1, 3} # ¼ýÀÚ 3°³·Î ÀÌ·ç¾îÁø ÁýÇÕ ÀÚ·áÇü
numbers
set([1, 2, 3, 1, 2]) # ¸®½ºÆ®·ÎºÎÅÍ ÁýÇÕÀ» »ý¼ºÇÔ
set("abcdefa") # ¹®ÀÚ¿µµ ½ÃÄö½ºÇüÀ̶ó¼ ÁýÇÕÇüÀ¸·Î º¯È¯ÀÌ °¡´ÉÇÏ´Ù
numbers = set() # ºñ¾îÀÖ´Â ÁýÇÕ »ý¼º
numbers = {2, 1, 3}
if 1 in numbers: # 1À̶ó´Â Ç׸ñÀÌ numbers ÁýÇÕ¿¡ Àִ°¡ °Ë»ç
print("ÁýÇÕ ¾È¿¡ 1ÀÌ ÀÖ½À´Ï´Ù.")
numbers = {2, 1, 3}
for x in numbers:
print(x, end=" ")
for x in sorted(numbers):
print(x, end=" ")
numbers = {1, 2, 3}
numbers.add(4)
numbers
numbers.remove(5)
numbers
a_list = ['hello', 'world', 'welcome', 'to', 'python']
'python' in a_list
A = {1, 2, 3}
B = {1, 2, 3}
A == B
A = {1, 2, 3, 4, 5}
B = {1, 2, 3}
B < A
a_set = {1, 5, 4, 3, 7, 4 } # 6°³ Ç׸ñÀ¸·Î ÁýÇÕ »ý¼º
len(a_set) # Ç׸ñÀÇ °³¼ö´Â Áߺ¹À» Á¦¿ÜÇϸé 5
max(a_set) # Ç׸ñ °¡¿îµ¥ °¡Àå Å« ¼ö´Â 7
min(a_set) # Ç׸ñ °¡¿îµ¥ °¡Àå ÀÛÀº ¼ö´Â 1
sorted(a_set) # Ç׸ñÀ» Á¤·ÄÇÏ¿© ¸®½ºÆ® ¸¸µç´Ù. Áߺ¹Àº Á¦°Å
sum(a_set) # Áߺ¹ ¿ø¼Ò´Â Çϳª¸¸ »ç¿ëµÇ¹Ç·Î Àüü ÇÕÀº 20
a_set = { 1, 0, 2, 3, 3}
all(a_set), any(a_set) # a_setÀÌ ¸ðµÎ TrueÀΰ¡?, a_set¿¡ 0ÀÌ Àִ°¡? °Ë»ç
A = {1, 2, 3}
B = {3, 4, 5}
A | B # ÇÕÁýÇÕ ¿¬»ê
A.union(B) # ÇÕÁýÇÕ ¸Þ¼Òµå
A & B # ±³ÁýÇÕ ¿¬»ê
A.intersection(B) # ±³ÁýÇÕ ¸Þ¼Òµå
A - B # Â÷ÁýÇÕ ¿¬»ê
A.difference(B) # Â÷ÁýÇÕ ¸Þ¼Òµå
A ^ B # ´ëĪ Â÷ÁýÇÕ ¿¬»ê
A.symmetry_difference(B)
partyA = set(["Park", "Kim", "Lee"])
partyB = set(["Park", "Choi"])
print("2°³ÀÇ ÆÄƼ¿¡ ¸ðµÎ Âü¼®ÇÑ »ç¶÷Àº ´ÙÀ½°ú °°½À´Ï´Ù. ")
print ( partyA.intersection(partyB))
# ´Ü¾î¿¡¼ ±¸µÎÁ¡À» Á¦°ÅÇÏ°í ¼Ò¹®ÀÚ·Î ¸¸µç´Ù.
def process(w):
output =""
for ch in w:
if( ch.isalpha() ):
output += ch
return output.lower()
words = set()
# ÆÄÀÏÀ» ¿¬´Ù.
fname = input("ÀÔ·Â ÆÄÀÏ À̸§: ")
file = open(fname, "r")
# ÆÄÀÏÀÇ ¸ðµç ÁÙ¿¡ ´ëÇÏ¿© ¹Ýº¹ÇÑ´Ù.
for line in file:
lineWords = line.split()
for word in lineWords:
words.add(process(word)) # ´Ü¾î¸¦ ¼¼Æ®¿¡ Ãß°¡ÇÑ´Ù.
print("»ç¿ëµÈ ´Ü¾îÀÇ °³¼ö=", len(words))
print(words)
num = 10
divisors = []
for i in range(2, num):
if num % i == 0:
divisors.append(i)
print(num,'ÀÇ Áø¾à¼ö :', divisors)
def get_divisors(num): # numÀÇ ¾à¼ö¸¦ ÁýÇÕÇüÀ¸·Î ¹ÝȯÇÔ
divisors = set()
for i in range(2, num):
if num % i == 0:
divisors.add(i)
return divisors
x = 48
print(x,'ÀÇ Áø¾à¼ö :', get_divisors(x))
y = 60
print(y,'ÀÇ Áø¾à¼ö :', get_divisors(y))
A = get_divisors(x)
B = get_divisors(y)
print(A.intersection(B))
print(x, y,'ÀÇ ÃÖ´ë°ø¾à¼ö :', max(A.intersection(B)))