Funkcja zwracająca słownik - ile gier jest pogrupowanych wg. gatunku ?

0

English:
#How many games are there grouped by genre?
#Expected name of the function: count_grouped_by_genre(file_name)
#Expected output of the function: a dictionary with this structure: { [genre] : [count] }
#Detailed description: return a dictionary where each genre is associated with the count of the games of its genre

Polish:

Ile gier jest pogrupowanych według gatunków?

Wyświetlana nazwa funkcji: count_grouped_by_genre (file_name)

Wyświetlane wyniki funkcji: słownik o tej strukturze: {[gatunek]: [liczba]}

Szczegółowy opis: zwróć słownik, w którym każdy gatunek jest powiązany z liczbą gier swojego gatunku

Napisałem taką funkcję:


```def count_grouped_by_genre(file_name):
    genres = []
    dictionary = {}
    count = 0
    with open(file_name, 'rt') as file:
        for line in file.readlines():
            line= line.split("\t")
            genres.append(line[3])
        print(genres)
        for key, value in dictionary.items():
            for i in range(0, len(genres)):
                #value = count[i]
                if genres[i] == "Survival game":
                    #count += 1
                    dictionary[key] += 1
                elif genres[i] == "RPG":
                    count += 1
                elif genres[i] == "Action-adventure":
                    count += 1
                elif genres[i] == "First-person shooter":
                    count += 1
                elif genres[i] == "Simulation":
                    count += 1
                elif genres[i] == "Real-time strategy":
                    count += 1
                elif genres[i] == "Sandbox":
                    count += 1
            #print(str(key) + " : " + str(value))
            print(dictionary)
            #for key, value in dictionary.items():
             #   print(str(key) + ":" + str(value))
            #return dictionary


Pomoglibyście mi ją dokończyć? 
Dołączam plik game_stat.txt z informacjami nt. gier.
2

Wiesz co? Ten bootcamp nic Ci nie da, jak przy każdym zadaniu nie pogłówkujesz.

Masz, bo mi się chciało :P

def count_grouped_by_genre(file_name):
	resultDict = {}
	
	with open(file_name, 'rt') as file:
		for line in file.readlines():
			line = line.split("\t")
			key = line[3]
			
			resultDict[key] = resultDict.get(key, 0) + 1
			
	return resultDict
	
print (count_grouped_by_genre ("game_stat.txt"))
1

Dodam tylko że bardzo często w 'podobnych' zadaniach gdzie trzeba coś zliczyć a zwłaszcza pogrupować warto skorzystać z:

from collections import defaultdict

w powyższym przypadku też oczywiście można przerobić to na defaultdict choć różnica będzie subtelna.

Jeśli chodzi o grupowanie to mały przykład.
Chcemy stworzyć słownik który zawiera informacje jaki typ gry ma jakich przedstawicieli można zrobić jak poniżej:

from collections import defaultdict


def count_grouped_by_genre(file_name):
    result_dict = defaultdict(list)

    with open(file_name, 'rt') as file:
        for line in file.readlines():
            game, number, date, genre, company = line.split('\t')
            result_dict[genre].append(game)

    return dict(result_dict)


print(count_grouped_by_genre("game_stat.txt"))

1 użytkowników online, w tym zalogowanych: 0, gości: 1