2021.06.25
7. Working with External Libraries (Tutorial, Exercise 2/3)
- 그래프 수정
1) Add the title "Results of 500 slot machine pulls"
2) Make the y-axis start at 0.
3) Add the label "Balance" to the y-axisdef prettify_graph(graph): """Modify the given graph according to Jimmy's requests: add a title, make the y-axis start at 0, label the y-axis. (And, if you're feeling ambitious, format the tick marks as dollar amounts using the "$" symbol.) """ graph.set_title("Results of 500 slot machine pulls") # Complete steps 2 and 3 here graph.set_title("Results of 500 slot machine pulls") graph.set_ylim(0, 400) graph.set_ylabel("Balance") graph = jimmy_slots.get_graph() prettify_graph(graph)
y축 레이블 수정
# An array of the values displayed on the y-axis (150, 175, 200, etc.) ticks = graph.get_yticks() # Format those values into strings beginning with dollar sign new_labels = ['${}'.format(int(amt)) for amt in ticks]
- 오류 수정하기
This is a very challenging problem. Don't forget that you can receive a hint!
Luigi is trying to perform an analysis to determine the best items for winning races on the Mario Kart circuit. He has some data in the form of lists of dictionaries that look like...
[
{'name': 'Peach', 'items': ['green shell', 'banana', 'green shell',], 'finish': 3},
{'name': 'Bowser', 'items': ['green shell',], 'finish': 1},
{'name': None, 'items': ['mushroom',], 'finish': 2},
{'name': 'Toad', 'items': ['green shell', 'mushroom'], 'finish': 1},
]
'items' is a list of all the power-up items the racer picked up in that race, and 'finish' was their placement in the race (1 for first place, 3 for third, etc.).
He wrote the function below to take a list like this and return a dictionary mapping each item to how many times it was picked up by first-place finishers# Import luigi's full dataset of race data from learntools.python.luigi_analysis import full_dataset # Fix me! def best_items(racers): winner_item_counts = {} for i in range(len(racers)): # The i'th racer dictionary racer = racers[i] # We're only interested in racers who finished in first if racer['finish'] == 1: for i in racer['items']: # Add one to the count for this item (adding it to the dict if necessary) if i not in winner_item_counts: winner_item_counts[i] = 0 winner_item_counts[i] += 1 # Data quality issues :/ Print a warning about racers with no name set. We'll take care of it later. if racer['name'] is None: print("WARNING: Encountered racer with unknown name on iteration {}/{} (racer = {})".format( i+1, len(racers), racer['name']) ) return winner_item_counts # Try analyzing the imported full dataset best_items(full_dataset)
데이터 일부만 실행시켰을 땐 발생하지 않던 오류가 전체 데이터 셋을 넣으니 오류가 발생했다.
알고보니 racer['finish']==1
도 만족하고, racer['name'] is None
도 만족하는 데이터가 있어 i
가 중복으로 사용되어서 그랬다.
코드 수정과 3번은 내일,,