Skip to content

Instantly share code, notes, and snippets.

@nenodias
Created March 2, 2019 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nenodias/84d3a75926b6434d026b0672d44c1a19 to your computer and use it in GitHub Desktop.
Save nenodias/84d3a75926b6434d026b0672d44c1a19 to your computer and use it in GitHub Desktop.
AceleraDev Python para Web - Processe dados dos jogadores do FIFA© 2017 usando Python
# coding: utf-8
import csv
import collections
import functools
# Todas as perguntas são referentes ao arquivo `data.csv`
# Você ** não ** pode utilizar o pandas e nem o numpy para este desafio.
def gen_csv():
def fn():
with open('data.csv', 'r') as f:
headers = None
for row in csv.reader(f):
if headers is None:
headers = row
continue
yield({headers[i]: row[i] for i in range(len(row)) })
return fn()
# **Q1.** Quantas nacionalidades (coluna `nationality`) diferentes existem no arquivo?
#
def q_1():
return len(collections.Counter([i['nationality'] for i in gen_csv() ]).keys())
# **Q2.** Quantos clubes (coluna `club`) diferentes existem no arquivo?
def q_2():
return len(collections.Counter([i['club'] for i in gen_csv() ]).keys())
# **Q3.** Liste o nome completo dos 20 primeiros jogadores de acordo com a coluna `full_name`.
def q_3():
return [i['full_name'] for idx, i in enumerate(gen_csv()) if idx < 20]
# **Q4.** Quem são os top 10 jogadores que ganham mais dinheiro (utilize as colunas `full_name` e `eur_wage`)?
def q_4():
def money(it):
return it[1]
return [i[0] for i in sorted([(i['full_name'], float(i['eur_wage']) ) for idx, i in enumerate(gen_csv())], key=money, reverse=True)][:10]
# **Q5.** Quem são os 10 jogadores mais velhos?
def q_5():
def age(it):
return it[1]
return [i[0] for i in sorted([(i['full_name'], float(i['age']) ) for idx, i in enumerate(gen_csv())], key=age, reverse=True)][:10]
# **Q6.** Conte quantos jogadores existem por idade. Para isso, construa um dicionário onde as chaves são as idades e os valores a contagem.
def q_6():
return { int(it[0]): it[1] for it in collections.Counter([i['age'] for i in gen_csv() ]).most_common() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment