과제를 위해 저는 올림픽 국가에 관한 정보와 메달 수를 검색하는 프로그램을 만들고 있습니다. 내 기능의문자열 키와 튜플 값을 사용하여 파일에서 독해를 읽는 방법은 무엇입니까?
하나는이 형식으로 목록을 간다 :
기능은 키와 국가의 이름으로 사전에이 목록을 통해 이동하고 저장해야Country,Games,Gold,Silver,Bronze
AFG,13,0,0,2
ALG,15,5,2,8
ARG,40,18,24,28
ARM,10,1,2,9
ANZ,2,3,4,5
, 나머지 네 개의 항목 터플로. 여기
내가 지금까지 함께 일하고 것입니다 : 항목이 대신이{'AFG': (13, 0, 0, 2)}
같은 것을보고 지금
def medals(string):
'''takes a file, and gathers up the country codes and their medal counts
storing them into a dictionary'''
#creates an empty dictionary
medalDict = {}
#creates an empty tuple
medalCount =()
#These following two lines remove the column headings
with open(string) as fin:
next(fin)
for eachline in fin:
code, medal_count = eachline.strip().split(',',1)
medalDict[code] = medal_count
return medalDict
은, 의도, 난
받고 있어요
{'AFG': '13,0,0,2'}
튜플이 아닌 문자열로 저장되는 것처럼 보입니다. 그것은 무엇과 관련이 있습니까?
medalDict[code] = medal_count
코드 줄? 나는 그것을 튜플에 대한 분리 된 정수 값으로 깔끔하게 변환하는 방법을 너무 확신하지 못한다.