###Dict

data_dict = {제목 : 값, 제목2, 값2}   이름이 있는 값 생성
dict(제목=값, 제목=값, 제목=값)   딕셔너리 생성
dict(data_tuple)   튜플 딕셔너리로 변환
data_dict[제목]   제목에 맞는 값을 출력함
datga_dict.update({이름 : 값, 이름:값})   추가 or 수정
del data_dict[이름]   이름을 가진것 삭제
data_dict.pop(이름)   위와 같음같음
data_dict.clear()   다 삭제
이름 in data_dict   이름있으면 true
이름 nor in data_dict   이름 없으면 true
data_dict.items   리스트 튜플 형태로 모든 값 반환
data_dict.keys   리스트 형태로 키워드 반환
data_dict.values   리스트 형태로 값 반환

###  dict 바꾸기

data_tuple = (("홍길동", 20), ("이순신", 45), ("강감찬", 35))
data_dict = dict(data_tuple)
print("data_dict:{0} {1}".format(type(data_dict), data_dict))

data_list = [("홍길동", 20), ("이순신", 45), ("강감찬", 35)]
data_dict2 = dict(data_list)
print("data_dict:{0} {1}".format(type(data_dict2), data_dict2))

data_set = {("홍길동", 20), ("이순신", 45), ("강감찬", 35)}
data_dict3 = dict(data_set)
print("data_dict:{0} {1}".format(type(data_dict3), data_dict3))

### dict 추가 or 수정

data_dict1 = {
	"홍길동": 20,
    "이순신": 45,
    "강감찬": 35
}

print("data_dict1: {0} {1}".format(type(data_dict1), data_dict1))

data_dict1["을지문덕"] = 40  //객체 이름[ 중복되ㅣ지 않은 키] = 값
print("data_dict1: {0} {1}".format(type(data_dict1), data_dict1))

data_dict1.update({"심사임당": 50,  "유관순": 16})
print(data_dict1: {0} {1}".format(type(data_dict1), data_dict1))

### item, keys, values

data_dict1 = {
	"홍길동": 20,
    "이순신": 45,
    "강감찬": 35
}

print("data_dict1: {0} {1}".format(type(data_dict1), data_dict1))

print("{0} {1}".format(type(data_dict1.items()), data_dict1.items()))
print("{0} {1}".format(type(data_dict1.keys()), data_dict1.keys()))
print("{0} {1}".format(type(data_dict1.values()), data_dict1.values()))

for key in data_dict1:
	print("key, data_dict1[key] => '{0}', {1}".format(key, data_dict1[key]))
    
for key in data_dict1.keys():
	print("key, data_dict1[key] => '{0}', {1}".format(key, data_dict1[key]))
   
for item in data_dict1.item():
	print("item[0], item[1] => '{0}, {1}".format(item[0], item[1]))

for key, value in data_dict.items():
	print("key, value => '{0}, {1}".format(key, value))

for value in data_dict.values():
	print("value => {0}".format(value))


///////////////////////////////////////
data_dict2 = {key : value for key, value in data_dict1.items()}
///////////////////////////////////////
    
    

### 학생들 총점 및 평균 구하기

# -*- coding: utf-8 -*-

scores = []

count = int(input("총 학생의 수를 입력하세요: "))

for i in range(1, count +1):
	score = {}
    score["name"] = input("학생의 이름을 입력하세요: ")
    score["kor"] = int(input("{0} 학생의 국어 점수를 입력하세요: ".format(score["name"])))
    score["mat"] = int(input("{0} 학생의 수학 점수를 입력하세요: ".format(score["name"])))
    score["emg"] = int(input("{0} 학생의 영어 점수를 입력하세요: ".format(score["name"])))
    scores.append(score)
    
for score in scores:
	total = 0
    for key in score:
    	if key != "name":
        	total += score[key]
    print("{0} => 총점: {1}, 평균: {2:0.2f}".format(score["name"], total, total/3))

kor_total, mat_total, eng_total = 0, 0, 0
for score in scores:
	for key in score:
    	if key == "kor":
        	kor_totla += score[key]
        elif key == "mat":
        	mat_total += score[key]
        elif key =="eng": 
        	eng_total += score[key]

### sorted value 값 적용

 sorted(d.items(), key=lambda x: x[1]

 

블로그 이미지

Or71nH

,