Merging dictionary with another dictionary - python

First off, sorry for any poor formatting or etiquette, I am new to python and stack overflow. I have seen other somewhat similar questions to this but all others merge the entire list. I only need parts of each dictionary.
I have a list of dictionaries that are ordered in pairs of games (team 1 then team 2) I need to combine a pair of each dictionary into 1 (0+1, 2+3, etc..)
Here is a sample of my complete list of dictionaries for reference
games_detailed =
[{'gameId': '8133',
'teamId': '26',
'fastBreakPoints': '16',
'pointsInPaint': '40',
'biggestLead': '15',
'secondChancePoints': '16',
'pointsOffTurnovers': '24',
'longestRun': '9',
'points': '116',
'fgm': '42',
'fga': '92',
'fgp': '45.7',
'ftm': '20',
'fta': '29',
'ftp': '69.0',
'tpm': '12',
'tpa': '42',
'tpp': '28.6',
'offReb': '10',
'defReb': '39',
'totReb': '49',
'assists': '26',
'pFouls': '28',
'steals': '10',
'turnovers': '15',
'blocks': '8',
'plusMinus': '4',
'min': '240:00',
'homeWin': '0',
'homeLoss': '0',
'home': '0',
'teamName': 'Orlando Magic'},
{'gameId': '8133',
'teamId': '1',
'fastBreakPoints': '14',
'pointsInPaint': '32',
'biggestLead': '4',
'secondChancePoints': '25',
'pointsOffTurnovers': '22',
'longestRun': '8',
'points': '112',
'fgm': '34',
'fga': '92',
'fgp': '37.0',
'ftm': '34',
'fta': '41',
'ftp': '82.9',
'tpm': '10',
'tpa': '32',
'tpp': '31.2',
'offReb': '19',
'defReb': '42',
'totReb': '61',
'assists': '20',
'pFouls': '25',
'steals': '8',
'turnovers': '23',
'blocks': '4',
'plusMinus': '-4',
'min': '240:00',
'homeWin': '0',
'homeLoss': '1',
'home': '1',
'teamName': 'Atlanta Hawks'},
{'gameId': '8134',
'teamId': '24',
'fastBreakPoints': '11',
'pointsInPaint': '52',
'biggestLead': '18',
'secondChancePoints': '17',
'pointsOffTurnovers': '19',
'longestRun': '14',
'points': '90',
'fgm': '38',
'fga': '83',
'fgp': '45.8',
'ftm': '9',
'fta': '19',
'ftp': '47.4',
'tpm': '5',
'tpa': '23',
'tpp': '21.7',
'offReb': '13',
'defReb': '41',
'totReb': '54',
'assists': '23',
'pFouls': '28',
'steals': '12',
'turnovers': '24',
'blocks': '6',
'plusMinus': '6',
'min': '240:00',
'homeWin': '0',
'homeLoss': '0',
'home': '0',
'teamName': 'New York Knicks'},
{'gameId': '8134',
'teamId': '10',
'fastBreakPoints': '11',
'pointsInPaint': '30',
'biggestLead': '7',
'secondChancePoints': '9',
'pointsOffTurnovers': '24',
'longestRun': '14',
'points': '84',
'fgm': '26',
'fga': '80',
'fgp': '32.5',
'ftm': '22',
'fta': '25',
'ftp': '88.0',
'tpm': '10',
'tpa': '39',
'tpp': '25.6',
'offReb': '10',
'defReb': '31',
'totReb': '41',
'assists': '17',
'pFouls': '18',
'steals': '12',
'turnovers': '22',
'blocks': '3',
'plusMinus': '-6',
'min': '240:00',
'homeWin': '0',
'homeLoss': '1',
'home': '1',
'teamName': 'Detroit Pistons'},
Intended New Dictionary =
{'gameId' : '8133', 'away_team' : 'Orlando Magic', 'away_points' : '116',
'home_team' : 'Atlanta Hawks', 'home_points' : '112', 'spread' : '-4',
'home_win' : '0', 'home_loss' : 1}
Here is the code I have ran in which I am getting an index error back. My thoughts were to create a dictionary for each away game because they are listed before each paired home game. Then, for the home game I was going to .update(temp_dict) the temporary dict I created in each loop to my main list. Note(I would welcome any criticism or critiques to my code. Still very early in all of this. Thanks!)
game_list = []
i = 1
for game in games_detailed:
if int(games_detailed[i-1]['home']) == 0:
temp_dict = {}
temp_dict['gameId'] = games_detailed[i-1]['gameId']
temp_dict['away_team'] = games_detailed[i-1]['teamName']
temp_dict['away_points'] = games_detailed[i-1]['points']
game_list.append(temp_dict)
elif int(games_detailed[i-1]['home']) == 1:
temp_dict = {}
temp_dict['home_team'] = games_detailed[i-1]['teamName']
temp_dict['home_points'] = games_detailed[i-1]['points']
temp_dict['spread'] = games_detailed[i-1]['plusMinus']
temp_dict['home_win'] = games_detailed[i-1]['homeWin']
temp_dict['home_loss'] = games_detailed[i-1]['homeLoss']
if i == 1:
count = i-1
else:
count = int(i/2)
game_list[count].update(temp_dict)
i += 1

If you want to work with pairs 0+1, 2+3, etc, then you can use zip() with [0::2] and [1::2]
game_list = []
for team1, team2 in zip(games_detailed[0::2], games_detailed[1::2]):
# get values from both teams and create one dictionary
game = {}
# team1 - away
game['gameId'] = team1['gameId']
game['away_team'] = team1['teamName']
game['away_points'] = team1['points']
# etc.
# team2 - home
game['home_team'] = team2['teamName']
game['home_points'] = team2['points']
game['spread'] = team2['plusMinus']
game['home_win'] = team2['homeWin']
# etc.
game_list.append(game)
Eventually you will have to check team1['home'] team2['home'] and change order
if team1['home'] == 1:
team1, team2 = team2, team1
EDIT:
Full working code with example data
games_detailed = [
{'gameId': '8133',
'teamId': '26',
'fastBreakPoints': '16',
'pointsInPaint': '40',
'biggestLead': '15',
'secondChancePoints': '16',
'pointsOffTurnovers': '24',
'longestRun': '9',
'points': '116',
'fgm': '42',
'fga': '92',
'fgp': '45.7',
'ftm': '20',
'fta': '29',
'ftp': '69.0',
'tpm': '12',
'tpa': '42',
'tpp': '28.6',
'offReb': '10',
'defReb': '39',
'totReb': '49',
'assists': '26',
'pFouls': '28',
'steals': '10',
'turnovers': '15',
'blocks': '8',
'plusMinus': '4',
'min': '240:00',
'homeWin': '0',
'homeLoss': '0',
'home': '0',
'teamName': 'Orlando Magic'},
{'gameId': '8133',
'teamId': '1',
'fastBreakPoints': '14',
'pointsInPaint': '32',
'biggestLead': '4',
'secondChancePoints': '25',
'pointsOffTurnovers': '22',
'longestRun': '8',
'points': '112',
'fgm': '34',
'fga': '92',
'fgp': '37.0',
'ftm': '34',
'fta': '41',
'ftp': '82.9',
'tpm': '10',
'tpa': '32',
'tpp': '31.2',
'offReb': '19',
'defReb': '42',
'totReb': '61',
'assists': '20',
'pFouls': '25',
'steals': '8',
'turnovers': '23',
'blocks': '4',
'plusMinus': '-4',
'min': '240:00',
'homeWin': '0',
'homeLoss': '1',
'home': '1',
'teamName': 'Atlanta Hawks'},
{'gameId': '8134',
'teamId': '24',
'fastBreakPoints': '11',
'pointsInPaint': '52',
'biggestLead': '18',
'secondChancePoints': '17',
'pointsOffTurnovers': '19',
'longestRun': '14',
'points': '90',
'fgm': '38',
'fga': '83',
'fgp': '45.8',
'ftm': '9',
'fta': '19',
'ftp': '47.4',
'tpm': '5',
'tpa': '23',
'tpp': '21.7',
'offReb': '13',
'defReb': '41',
'totReb': '54',
'assists': '23',
'pFouls': '28',
'steals': '12',
'turnovers': '24',
'blocks': '6',
'plusMinus': '6',
'min': '240:00',
'homeWin': '0',
'homeLoss': '0',
'home': '0',
'teamName': 'New York Knicks'},
{'gameId': '8134',
'teamId': '10',
'fastBreakPoints': '11',
'pointsInPaint': '30',
'biggestLead': '7',
'secondChancePoints': '9',
'pointsOffTurnovers': '24',
'longestRun': '14',
'points': '84',
'fgm': '26',
'fga': '80',
'fgp': '32.5',
'ftm': '22',
'fta': '25',
'ftp': '88.0',
'tpm': '10',
'tpa': '39',
'tpp': '25.6',
'offReb': '10',
'defReb': '31',
'totReb': '41',
'assists': '17',
'pFouls': '18',
'steals': '12',
'turnovers': '22',
'blocks': '3',
'plusMinus': '-6',
'min': '240:00',
'homeWin': '0',
'homeLoss': '1',
'home': '1',
'teamName': 'Detroit Pistons'},
]
game_list = []
for team1, team2 in zip(games_detailed[0::2], games_detailed[1::2]):
# get values from both teams and create one dictionary
if team1['home'] == 1:
team1, team2 = team2, team1
game = {}
# team1 - away
game['gameId'] = team1['gameId']
game['away_team'] = team1['teamName']
game['away_points'] = team1['points']
# etc.
# team2 - home
game['home_team'] = team2['teamName']
game['home_points'] = team2['points']
game['spread'] = team2['plusMinus']
game['home_win'] = team2['homeWin']
# etc.
game_list.append(game)
for game in game_list:
print(game)
Result:
{'gameId': '8133', 'away_team': 'Orlando Magic', 'away_points': '116', 'home_team': 'Atlanta Hawks', 'home_points': '112', 'spread': '-4', 'home_win': '0'}
{'gameId': '8134', 'away_team': 'New York Knicks', 'away_points': '90', 'home_team': 'Detroit Pistons', 'home_points': '84', 'spread': '-6', 'home_win': '0'}

You could use defaultdict(dict) for grouping by game and create the sub-dictionaries for home or away teams then update it into the default dict based on gameId
from collections import defaultdict
#Name mappings
away_keys = list(zip(['gameId','away_team','away_points'], #New key names
['gameId','teamName','points'])) #Old key names
home_keys = list(zip(['gameId','home_team','home_points','spread','home_win'],
['gameId','teamName','points','plusMinus','homeWin']))
games = defaultdict(dict)
for g in games_detailed:
if g['home']=='0':
d = {i:g[j] for i,j in away_keys}
else:
d = {i:g[j] for i,j in home_keys}
games[d['gameId']].update(d)
game_results = list(games.values())
game_results
[{'gameId': '8133',
'away_team': 'Orlando Magic',
'away_points': '116',
'home_team': 'Atlanta Hawks',
'home_points': '112',
'spread': '-4',
'home_win': '0'},
{'gameId': '8134',
'away_team': 'New York Knicks',
'away_points': '90',
'home_team': 'Detroit Pistons',
'home_points': '84',
'spread': '-6',
'home_win': '0'}]

Related

How can I remove the numbers in this list?

I need to remove the numbers (str) in a list I made for a project, and if is possible I want to save them in a variable. I'm a beginner at python, so I dont know how to do it. Can anyone help me?
names = ['MATEO', '3869', 'AGUSTIN', '2641', 'BENJAMIN', '2337', 'TOMAS', '2235', 'LUCAS', '2146', 'SANTIAGO', '2124', 'GASPAR', '1922', 'JOAQUIN', '1750', 'VICENTE', '1717', 'MATIAS', '1712', 'MAXIMILIANO', '1704', 'MARTIN', '1587', 'ALONSO', '1573', 'FACUNDO', '1439', 'LUCIANO', '1402', 'EMILIANO', '1296', 'JOSE', '1287', 'CRISTOBAL', '1267', 'MAXIMO', '1248', 'BRUNO', '1227', 'JULIAN', '1126', 'LIAM', '1088', 'DANTE', '1083', 'GABRIEL', '1082', 'DIEGO', '1052', 'NICOLAS', '1021', 'SANTINO', '950', 'LEON', '946', 'SIMON', '940', 'JUAN', '901', 'SEBASTIAN', '855', 'RENATO', '717', 'IAN', '701', 'THOMAS', '684', 'GAEL', '673', 'AMARO', '670', 'RAFAEL', '656', 'DANIEL', '652', 'PEDRO', '652', 'IGNACIO', '640', 'FELIPE', '638', 'SAMUEL', '578', 'ANGEL', '565', 'BASTIAN', '564', 'FRANCO', '560', 'VALENTIN', '546', 'THIAGO', '530', 'MARIANO', '525', 'LUIS', '502', 'CLEMENTE', '482', 'FRANCISCO', '479', 'DAVID', '464', 'MATTEO', '464', 'JAVIER', '462', 'ISAAC', '444', 'EMILIO', '435', 'PABLO', '434', 'DYLAN', '432', 'DAMIAN', '425', 'CARLOS', '415', 'ALEXANDER', '415', 'NOAH', '411', 'SALVADOR', '409', 'BALTAZAR', '401', 'FERNANDO', '401', 'MATHEO', '381', 'ELIAN', '372', 'AARON', '348', 'LEONARDO', '337', 'GUSTAVO', '335', 'ESTEBAN', '330', 'ELIAS', '328', 'BORJA', '322', 'MANUEL', '307', 'JORGE', '302', 'VALENTINO', '301', 'CRISTIAN', '299', 'ANDRES', '297', 'OLIVER', '296', 'MIGUEL', '277', 'VICTOR', '274', 'ALEJANDRO', '269', 'ETHAN', '238', 'RODRIGO', '238', 'EDUARDO', '231', 'LAUTARO', '231', 'CAMILO', '229', 'JOSUE', '228', 'MATHIAS', '227', 'ALAN', '217', 'FABIAN', '217', 'RAIMUNDO', '216', 'JESÚS', '205', 'EITHAN', '198', 'ALVARO', '194', 'LEANDRO', '183', 'IKER', '177', 'CHRISTOPHER', '174', 'EZEQUIEL', '173', 'ISAIAS', '173', 'MILAN', '173', 'LIAN', '172', 'LUKAS', '170', 'ISMAEL', '169', 'RICARDO', '168', 'CRISTOPHER', '167', 'HECTOR', '162', 'ABRAHAM', '162', 'GUILLERMO', '157', 'LORENZO', '153', 'PASCUAL', '151', 'JEAN', '150', 'AXEL', '148', 'EMMANUEL', '148', 'SERGIO', '148', 'DEMIAN', '147', 'ALFONSO', '142', 'ANTONIO', '140', 'ALEX', '139', 'OSCAR', '138', 'JEREMIAS', '132', 'GONZALO', '128', 'ANTHONY', '127', 'MOISÉS', '126', 'JONATHAN', '125', 'DOMINGO', '124', 'ARTURO', '122', 'NAHUEL', '121', 'MAURICIO', '120', 'MARIO', '119', 'ERICK', '119', 'THEO', '118', 'MARCELO', '116', 'AMARU', '114', 'AUGUSTO', '114', 'PIERO', '114', 'ADRIAN', '114', 'LYAN', '112', 'ABDIEL', '110', 'AUSTIN', '109', 'CÉSAR', '109', 'ROMAN', '109', 'IVAN', '108', 'ENZO', '107', 'KEVIN', '104', 'CLAUDIO', '99', 'EMANUEL', '99', 'MAX', '98', 'CHRISTIAN', '98', 'ALEXIS', '95', 'GASTÓN', '94', 'DEREK', '93', 'FEDERICO', '92', 'MARCO', '91', 'EYDAN', '89', 'ANIBAL', '87', 'PATRICIO', '86', 'RAÚL', '82', 'OCTAVIO', '78', 'JACOB', '78', 'FÉLIX', '78', 'DARÍO', '78', 'LYAM', '77', 'ROBERTO', '75', 'ARIEL', '74', 'ZAID', '72', 'WILLIAM', '72', 'EMIR', '71', 'ISRAEL', '71', 'STEFANO', '71', 'DILAN', '70', 'JAIME', '70', 'BAUTISTA', '70', 'WILLIAMS', '69', 'SANTI', '68', 'EVAN', '68', 'ADRIEL', '68', 'BALTASAR', '66', 'CRISTOFER', '66', 'JHON', '65', 'ELUNEY', '64', 'HUGO', '63', 'CALEB', '63', 'JOSEPH', '63', 'PAOLO', '63', 'KYLIAN', '63', 'SAMIR', '62', 'TAHIEL', '62', 'NEHEMIAS', '62', 'JEREMY', '61', 'JOSIAS', '60', 'LEONEL', '59', 'ANDER', '59', 'IÑAKI', '59', 'OMAR', '59', 'ANGELO', '57', 'ALESSANDRO', '57', 'JOEL', '57', 'BELTRÁN', '56', 'NATHAN', '56', 'JOHN', '56', 'JOSHUA', '56', 'MILOVAN', '55', 'EFRAÍN', '55', 'JORDAN', '55', 'EDWARD', '54', 'HANS', '54', 'GIOVANNI', '53', 'ANTU', '53', 'ARON', '53', 'JARED', '53', 'LOGAN', '52', 'JOHAN', '51', 'MARCOS', '51', 'TOBIAS', '51', 'LEONIDAS', '51', 'EVANS', '50', 'MISAEL', '50', 'JULIO', '49', 'PAULO', '48', 'RODOLFO', '48', 'EDGAR', '48', 'ADOLFO', '46', 'MAURO', '46', 'RUBEN', '46', 'ABEL', '45', 'IZAN', '45', 'ENRIQUE', '44', 'RICHARD', '44', 'ALBERTO', '44', 'NICANOR', '43', 'TEO', '43', 'DANILO', '43', 'ELIEL', '42', 'LUCA', '42', 'EXEQUIEL', '42', 'EIDAN', '42', 'SAID', '41', 'DORIAN', '41', 'LUAN', '41', 'MIKE', '40', 'DERECK', '40', 'INTI', '40', 'SAÚL', '40', 'ALONZO', '39', 'MICHAEL', '39', 'GERARDO', '38', 'LUCCA', '37', 'HERNÁN', '37', 'ANDERSON', '37', 'YAHIR', '37', 'FABIO', '36', 'AMIR', '36', 'ISAI', '35', 'JAYDEN', '35', 'EINAR', '35', 'NELSON', '35', 'ANTUAN', '35', 'GERMÁN', '34', 'LIONEL', '33', 'ANDRE', '33', 'AUKAN', '33', 'BRAYAN', '32', 'BRANDON', '32', 'PATRICK', '32', 'SILVESTRE', '31', 'TADEO', '31', 'ALEN', '31', 'STEVEN', '31', 'PAUL', '30', 'DOMINIC', '30', 'NATANAEL', '30', 'LUKA', '30', 'ADRIANO', '30', 'RAMIRO', '30', 'JAMES', '30', 'MATTHEW', '30', 'DEMIR', '30', 'MARVENS', '29', 'DARIEL', '29', 'ALFREDO', '29', 'GADIEL', '29', 'LUCCIANO', '29', 'ULISES', '28', 'JOAN', '28', 'ELIAM', '28', 'ADAM', '28', 'URIEL', '28', 'YAIR', '28', 'LUCCAS', '28', 'BRYAN', '28', 'CHRIS', '28', 'MARC', '27', 'BERNARDO', '27', 'ROBERT', '27', 'IBRAHIM', '27', 'TEODORO', '26', 'ANDY', '26', 'MASSIMO', '26', 'NEYTHAN', '26', 'OWEN', '26', 'ERIK', '25', 'ELISEO', '25', 'JHOAN', '25', 'CIRO', '25', 'VLADIMIR', '24', 'EYTHAN', '24', 'BAYRON', '24', 'FRANCESCO', '24', 'JOAO', '24', 'YOEL', '24', 'JAIRO', '24', 'ERIC', '23', 'KILIAN', '23', 'OSVALDO', '23', 'JAIR', '23', 'IHAN', '23', 'JUAQUIN', '23', 'FARID', '23', 'MAEL', '23', 'GIANLUCA', '22', 'IÑIGO', '22', 'FERRÁN', '22', 'ARTHUR', '22', 'NEITHAN', '22', 'JACK', '22', 'ANDREW', '22', 'ERNESTO', '22', 'GERALD', '22', 'WOOD', '22', 'ANTOINE', '21', 'FLAVIO', '21', 'EDISON', '21', 'RENÉ', '21', 'BRAULIO', '21', 'PRINCE', '21', 'BORIS', '21', 'NATHANIEL', '20', 'RAPHAEL', '20', 'JUSTIN', '20', 'TRISTÁN', '20', 'GIULIANO', '20', 'VINCENT', '20', 'LUCIO', '20', 'ADONIS', '20', 'LEO', '20', 'ITHAN', '19', 'APOLO', '19', 'VITTORIO', '19', 'NAIM', '19', 'KARIM', '19', 'ADÁN', '19', 'ALEXANDRO', '19', 'ANTHUAN', '18', 'DIDIER', '18', 'NAWEL', '18', 'AQUILES', '18', 'MAICOL', '18', 'FABRIZIO', '18', 'BALTHAZAR', '18', 'SANTY', '17', 'RYAN', '17', 'RAMÓN', '17', 'ELIEZER', '17', 'NÉSTOR', '17', 'FABRICIO', '17', 'DARIEN', '17', 'JONAS', '17', 'ADAMS', '17', 'JERÓNIMO', '17', 'SALOMÓN', '17', 'YAEL', '16', 'ALLAN', '16', 'ALDO', '16', 'KAI', '16', 'LUCIAN', '16', 'EDER', '16', 'IAM', '16', 'OSTIN', '16', 'BENICIO', '16', 'ZABDIEL', '16', 'ELLIOT', '15', 'XAVIER', '15', 'ELEAZAR', '15', 'ESTEFANO', '15', 'EDWIN', '15', 'GERÓNIMO', '15', 'MARCUS', '15', 'HENRY', '15', 'EDUARD', '15', 'EMERSON', '15', 'JUANPABLO', '14', 'GIOVANNY', '14', 'WLADIMIR', '14', 'LEVI', '14', 'WILSON', '14', 'GIAN', '14', 'AIDAN', '14', 'TYRONE', '14', 'JHAIR', '14', 'NEIZAN', '14', 'ITALO', '14', 'WALTER', '14', 'GIANFRANCO', '14', 'GAMALIEL', '14', 'DASTIN', '14', 'JADIEL', '14', 'ESAI', '14', 'ZAMIR', '13', 'JAEL', '13', 'RONALD', '13', 'ZAHID', '13', 'GREGORIO', '13', 'DONATO', '13', 'ROQUE', '13', 'ZAHIR', '13', 'DASTAN', '13', 'RAMSES', '13', 'ROBINSON', '12', 'ANYELO', '12', 'GIORGIO', '12', 'SAM', '12', 'JOSE-TOMAS', '12', 'OSMAN', '12', 'FIDEL', '12', 'BYRON', '12', 'ILIAN', '12', 'ORLANDO', '12', 'CRISTHIÁN', '12', 'MARLON', '12', 'MIRKO', '12', 'RALPH', '12', 'RAYAN', '12', 'STEFAN', '12', 'TYLER', '12', 'GREGORY', '12', 'JUNIOR', '12', 'JEISON', '12', 'VASCO', '12', 'ROOD', '12', 'NIKOLAS', '12', 'FRANCHESCO', '11', 'TIAGO', '11', 'EREN', '11', 'TOMMY', '11', 'BENITO', '11', 'FRANK', '11', 'DOMINICK', '11', 'ELIOT', '11', 'DEYLAN', '11', 'JEFFERSON', '11', 'ÉTIENNE', '11', 'ANTONY', '11', 'MAIKEL', '11', 'JHONNY', '11', 'NEWEN', '11', 'KEYLOR', '11', 'MAYKOL', '11', 'EITAN', '11', 'MAIKOL', '11', 'EIDER', '11', 'JULIANO', '11', 'KILLIAN', '11', 'ABDIAS', '11', 'CRESCENTE', '11', 'SALVATORE', '11', 'HASSAN', '11', 'RENZO', '11', 'VINCENZO', '10', 'BRUCE', '10', 'RAYMUNDO', '10', 'ALÍ', '10', 'YAMIR', '10', 'MILO', '10', 'ARLEY', '10', 'JOE', '10', 'NEFTALÍ', '10', 'ERWIN', '10', 'IANN', '10', 'STEPHANO', '10', 'MARCIAL', '10', 'SANDRO', '10', 'DAVE', '10', 'DAWENS', '10', 'LENIN', '10', 'BRIAN', '10', 'JADEN', '10', 'YEISON', '10', 'EZRA', '10', 'FILIPPO', '10', 'YULIANO', '10', 'YERIK', '10', 'FRANTZ', '10', 'IVAR', '10', 'YADIEL', '10', 'FRANKO', '10', 'WOODLEY', '10', 'JAHAZIEL', '10', 'ELYAN', '10', 'EDEN', '9', 'PASCAL', '9', 'TAYRON', '9', 'AIDEN', '9', 'NATANIEL', '9', 'JACOBO', '9', 'BAIRON', '9', 'DARWIN', '9', 'JIMMY', '9', 'KALETH', '9', 'ASHER', '9', 'BRADLEY', '9', 'ROGER', '9', 'GERARD', '9', 'JOB', '9', 'GUIDO', '9', 'JAZIEL', '9', 'DAWENSKY', '9', 'YEREMI', '9', 'SANTOS', '9', 'ROY', '9', 'CRISTIANO', '9', 'JHOEL', '9', 'DENZEL', '9', 'LION', '9', 'ARMANDO', '9', 'UZIEL', '9', 'KERVENS', '9', 'ILAN', '9', 'NAHUM', '9', 'MAGDIEL', '9', 'BRANCO', '9', 'SANTHIAGO', '9', 'BERNABÉ', '9', 'AWKÁN', '8', 'NEYMAR', '8', 'STEPHAN', '8', 'JAYSON', '8', 'ROLANDO', '8', 'FAUSTINO', '8', 'DÉNIS', '8', 'ALESSIO', '8', 'TAYLER', '8', 'JASON', '8', 'YORDAN', '8', 'NOAM', '8', 'EDINSON', '8', 'HORACIO', '8', 'NIKOLA', '8', 'PIERRE', '8', 'DANNY', '8', 'AYUN', '8', 'REINALDO', '8', 'BASTHIAN', '8', 'ARIAN', '8', 'ENOC', '8', 'YERAY', '8', 'MIQUEAS', '8', 'DEIVID', '8', 'THOMMAS', '8', 'MATTIAS', '8', 'VIGGO', '8', 'FAUSTO', '8', 'CHRIST', '8', 'RAGNAR', '8', 'FABRIZZIO', '8', 'KAEL', '8', 'GIANCARLO', '8', 'NATHANAEL', '8', 'FRANCIS', '8', 'ROMÉO', '8', 'MATTHEO', '7', 'EUGENIO', '7', 'TRAVIS', '7', 'MILLER', '7', 'GHAEL', '7', 'EDRIC', '7', 'SELIM', '7', 'LÁZARO', '7', 'NOEL', '7', 'ETHIÁN', '7', 'LOVENSKY', '7', 'PANGAL', '7', 'CARLO', '7', 'PHILIPPE', '7', 'MICHEL', '7', 'EMIL', '7', 'POLO', '7', 'ALLEN', '7', 'ABDEL', '7', 'CARLENS', '7', 'DENNIS', '7', 'LOUIS', '7', 'GIORDANO', '7', 'ANKATU', '7', 'CAETANO', '7', 'DANTTE', '7', 'REIMUNDO', '7', 'LUCKAS', '7', 'PELAYO', '7', 'KILIAM', '7', 'GIUSEPPE', '7', 'BRUNNO', '7', 'MATIA', '7', 'FABIANO', '7', 'TAYLOR', '7', 'LIFKO', '7', 'FRANKLIN', '7', 'AZIEL', '7', 'ALAIN', '7', 'JIA', '7', 'YULIAN', '7', 'ENMANUEL', '7', 'CARL', '7', 'GIANLUCAS', '7', 'DAYRON', '7', 'MAXIMILIAN', '7', 'GAHEL', '7', 'WENSLEY', '7', 'DARELL', '7', 'GENARO', '7', 'JUN', '6', 'EFRAÍM', '6', 'NIL', '6']
I was thinking in a def to use names.pop(x) with x as every number of index that is impair, cause the total of str is 1246, so the index quantity is 1247. But although I have tried many times, I haven't been able to get it right.
You can use a list comprehension:
[item for item in names if item.isnumeric()]
It also appears that the list contains a pattern where there's one name followed by one numeric value. If this is the case, you can use list slicing instead:
names[1::2]
These output (first three / last three elements):
['3869', '2641', '2337', ..., '6', '6', '6']
For instance, you can do something like this:
names = ['MATEO', '3869', 'AGUSTIN', '2641']
numbers = []
for string in names:
if string.isnumeric():
numbers.append(string)
>>> numbers
>>> ['3869', '2641']
Or you can use list comprehensions:
numbers = [string for string in names if string.isnumeric()]
You can use list comprehension and save its output to a variable, like this:
names = ['MATEO', '3869', 'AGUSTIN', '2641']
numbers = [item for item in names if item.isnumeric()]
>>> numbers
>>> ['3869', '2641']

Print value from list

This is the code
import soccer_data_api
soccer_data = soccer_data_api.SoccerDataAPI()
serie_a = soccer_data.serie_a()
print(serie_a)
This is the print result:
[{'team': 'Inter', 'pos': '1', 'points': '82', 'matches_played': '33',
'wins': '25', 'draws': '7', 'losses': '2', 'goals_for': '74',
'goals_against': '29', 'goal_diff': '+45', 'top_scorer': 'Romelu
Lukaku - 21'}, {'team': 'Milan', 'pos': '2', 'points': '69',
'matches_played': '34', 'wins': '21', 'draws': '6', 'losses': '7',
'goals_for': '62', 'goals_against': '41', 'goal_diff': '+21',
'top_scorer': 'Zlatan Ibrahimović - 15'}, {'team': 'Atalanta', 'pos':
'3', 'points': '68', 'matches_played': '33', 'wins': '20', 'draws':
'8', 'losses': '5', 'goals_for': '78', 'goals_against': '39',
'goal_diff': '+39', 'top_scorer': 'Luis Muriel - 19'}, {'team':
'Napoli', 'pos': '4', 'points': '66', 'matches_played': '33', 'wins':
'21', 'draws': '3', 'losses': '9', 'goals_for': '73', 'goals_against':
'37', 'goal_diff': '+36', 'top_scorer': 'Lorenzo Insigne - 17'},
{'team': 'Juventus', 'pos': '5', 'points': '66', 'matches_played':
'34', 'wins': '19', 'draws': '9', 'losses': '5', 'goals_for': '65',
'goals_against': '30', 'goal_diff': '+35', 'top_scorer': 'Cristiano
Ronaldo - 25'}, {'team': 'Lazio', 'pos': '6', 'points': '61',
'matches_played': '33', 'wins': '19', 'draws': '4', 'losses': '9',
'goals_for': '56', 'goals_against': '46', 'goal_diff': '+10',
'top_scorer': 'Ciro Immobile - 18'}, {'team': 'Roma', 'pos': '7',
'points': '55', 'matches_played': '33', 'wins': '16', 'draws': '7',
'losses': '10', 'goals_for': '58', 'goals_against': '51', 'goal_diff':
'+7', 'top_scorer': 'Jordan Veretout - 10'}, {'team': 'Sassuolo',
'pos': '8', 'points': '52', 'matches_played': '34', 'wins': '14',
'draws': '10', 'losses': '9', 'goals_for': '55', 'goals_against':
'50', 'goal_diff': '+5', 'top_scorer': 'Domenico Berardi - 14'},
{'team': 'Sampdoria', 'pos': '9', 'points': '42', 'matches_played':
'34', 'wins': '12', 'draws': '6', 'losses': '15', 'goals_for': '43',
'goals_against': '47', 'goal_diff': '-4', 'top_scorer': 'Fabio
Quagliarella - 11'}, {'team': 'Hellas Verona', 'pos': '10', 'points':
'42', 'matches_played': '33', 'wins': '11', 'draws': '9', 'losses':
'14', 'goals_for': '41', 'goals_against': '42', 'goal_diff': '-1',
'top_scorer': 'Antonín Barák - 7'}, {'team': 'Udinese', 'pos': '11',
'points': '39', 'matches_played': '32', 'wins': '10', 'draws': '9',
'losses': '14', 'goals_for': '38', 'goals_against': '44', 'goal_diff':
'-6', 'top_scorer': 'Rodrigo De Paul - 8'}, {'team': 'Bologna', 'pos':
'12', 'points': '38', 'matches_played': '34', 'wins': '10', 'draws':
'8', 'losses': '15', 'goals_for': '44', 'goals_against': '53',
'goal_diff': '-9', 'top_scorer': 'Roberto Soriano - 9'}, {'team':
'Genoa', 'pos': '13', 'points': '36', 'matches_played': '33', 'wins':
'8', 'draws': '12', 'losses': '13', 'goals_for': '37',
'goals_against': '48', 'goal_diff': '-11', 'top_scorer': 'Mattia
Destro - 11'}, {'team': 'Fiorentina', 'pos': '14', 'points': '34',
'matches_played': '33', 'wins': '8', 'draws': '10', 'losses': '15',
'goals_for': '42', 'goals_against': '54', 'goal_diff': '-12',
'top_scorer': 'Dušan Vlahović - 17'}, {'team': 'Spezia', 'pos': '15',
'points': '34', 'matches_played': '33', 'wins': '8', 'draws': '10',
'losses': '16', 'goals_for': '43', 'goals_against': '63', 'goal_diff':
'-20', 'top_scorer': "M'Bala Nzola - 9"}, {'team': 'Benevento', 'pos':
'16', 'points': '31', 'matches_played': '33', 'wins': '7', 'draws':
'10', 'losses': '17', 'goals_for': '37', 'goals_against': '68',
'goal_diff': '-31', 'top_scorer': 'Gianluca Lapadula - 6'}, {'team':
'Torino', 'pos': '17', 'points': '31', 'matches_played': '33', 'wins':
'6', 'draws': '13', 'losses': '13', 'goals_for': '46',
'goals_against': '56', 'goal_diff': '-10', 'top_scorer': 'Andrea
Belotti - 12'}, {'team': 'Cagliari', 'pos': '18', 'points': '31',
'matches_played': '34', 'wins': '8', 'draws': '7', 'losses': '18',
'goals_for': '39', 'goals_against': '56', 'goal_diff': '-17',
'top_scorer': 'João Pedro - 15'}, {'team': 'Parma', 'pos': '19',
'points': '20', 'matches_played': '32', 'wins': '3', 'draws': '11',
'losses': '19', 'goals_for': '36', 'goals_against': '70', 'goal_diff':
'-34', 'top_scorer': 'Juraj Kucka, Hernani - 7'}, {'team': 'Crotone',
'pos': '20', 'points': '18', 'matches_played': '33', 'wins': '5',
'draws': '3', 'losses': '26', 'goals_for': '42', 'goals_against':
'85', 'goal_diff': '-43', 'top_scorer': 'Simeon Nwankwo - 19'}]
I want to print like this:
Inter 1 82 so without key (team for example)
serie_a is a list of dicts, so change print(serie_a) to:
for x in serie_a:
print(f"{x['team']} {x['pos']} {x['points']}")
Output:
Inter 1 82
Milan 2 69
Atalanta 3 68
Napoli 4 66
Juventus 5 66
...
I would try it like this:
print(serie_a[0]["team"], serie_a[0]["pos"], serie_a[0]["points"])
The result should be like what you wrote:
Inter 1 82
Of course, if you want all teams printed, you can put it in a loop.
print(serie_a[0]['team'], serie_a[0]['pos'], serie_a[0]['points'])
Use [0] for the first entry in the list, to print the whole list you can use:
for i in serie_a:
print(i['team'], i['pos'], i['points'])
for i in serie_a:
print(f"{i['team']} {i['pos']} {i['points']}")

Converting a list of strings to integers [duplicate]

This question already has answers here:
How do I parse a string to a float or int?
(32 answers)
Closed 2 years ago.
I'm working on a project and one of the requirements that is needed for it requires me to convert the set of temperatures from the data but I can't figure out how to start it. I need to get it in Fahrenheit but my problem is that the data are strings inside a list of a list and I can't figure out how to convert that into an integer so I can change it into Fahrenheit.
All of the listed temperatures in the data are in Celsius.
Here's what I have so far.
import requests
import matplotlib.pyplot as plt
import fix_yahoo_finance as yf
link = "https://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt"
f = requests.get(link)
data = f.text
lines = data.split('\n')
el = [] # creates an empty list
for each in lines[8:159]: # gets rid of unnecessary information. Prints from 1880 to 2018
tempL = each.split()
if (tempL and tempL[0] != 'Year'): # gets rid of the excess data like the title
el.append(tempL[:13])
My result so far looks like this...
[['1880', '-29', '-18', '-11', '-20', '-12', '-23', '-21', '-9', '-16', '-23', '-20', '-23'], ['1881', '-16', '-17', '4', '4', '2', '-20', '-7', '-3', '-14', '-21', '-22', '-11'], ['1882', '14', '15', '3', '-19', '-16', '-26', '-21', '-6', '-10', '-25', '-16', '-25'], ['1883', '-32', '-39', '-13', '-17', '-20', '-13', '-8', '-15', '-21', '-14', '-22', '-16'], ['1884', '-16', '-8', '-37', '-43', '-37', '-41', '-35', '-26', '-27', '-24', '-30', '-29'], ['1885', '-59', '-30', '-25', '-42', '-42', '-44', '-36', '-32', '-24', '-19', '-20', '-5'], ['1886', '-42', '-45', '-39', '-28', '-26', '-39', '-22', '-34', '-26', '-29', '-31', '-27'], ['1887', '-73', '-52', '-34', '-39', '-32', '-24', '-23', '-32', '-23', '-33', '-23', '-34'], ['1888', '-38', '-36', '-41', '-22', '-22', '-18', '-9', '-16', '-10', '2', '1', '-6'], ['1889', '-11', '19', '8', '7', '-2', '-14', '-10', '-20', '-22', '-22', '-33', '-30'], ['1890', '-44', '-45', '-40', '-37', '-45', '-27', '-27', '-38', '-39', '-24', '-48', '-31'], ['1891', '-42', '-47', '-19', '-28', '-18', '-20', '-20', '-16', '-14', '-21', '-35', '-4'], ['1892', '-28', '-12', '-35', '-35', '-25', '-23', '-32', '-25', '-13', '-13', '-42', '-39'], ['1893', '-80', '-54', '-23', '-30', '-36', '-29', '-16', '-27', '-20', '-17', '-17', '-35'], ['1894', '-54', '-33', '-25', '-48', '-35', '-44', '-26', '-22', '-25', '-21', '-25', '-20'], ['1895', '-44', '-42', '-29', '-25', '-27', '-24', '-18', '-16', '-9', '-8', '-15', '-16'], ['1896', '-26', '-14', '-26', '-31', '-18', '-14', '-4', '-4', '-3', '11', '-8', '-4'], ['1897', '-15', '-15', '-14', '-2', '-5', '-16', '-7', '-11', '-8', '-12', '-19', '-19'], ['1898', '-3', '-30', '-52', '-33', '-32', '-21', '-24', '-25', '-22', '-33', '-38', '-25'], ['1899', '-19', '-40', '-34', '-21', '-23', '-33', '-18', '-7', '-3', '-5', '12', '-28'], ['1900', '-37', '-3', '1', '-12', '-10', '-13', '-13', '-10', '-5', '9', '-10', '-7'], ['1901', '-24', '-4', '8', '-4', '-17', '-13', '-16', '-20', '-22', '-31', '-17', '-28'], ['1902', '-20', '-3', '-29', '-30', '-35', '-35', '-30', '-32', '-29', '-29', '-39', '-46'], ['1903', '-26', '-4', '-24', '-43', '-44', '-46', '-37', '-49', '-49', '-48', '-45', '-52'], ['1904', '-68', '-58', '-51', '-55', '-56', '-51', '-54', '-50', '-53', '-40', '-22', '-35'], ['1905', '-40', '-59', '-24', '-36', '-33', '-31', '-29', '-22', '-19', '-25', '-10', '-19'], ['1906', '-28', '-31', '-16', '-4', '-24', '-21', '-25', '-20', '-27', '-19', '-39', '-16'], ['1907', '-42', '-51', '-27', '-39', '-47', '-44', '-37', '-35', '-33', '-24', '-49', '-48'], ['1908', '-44', '-31', '-54', '-46', '-41', '-41', '-40', '-46', '-36', '-45', '-50', '-49'], ['1909', '-69', '-47', '-51', '-60', '-56', '-53', '-45', '-31', '-35', '-37', '-31', '-55'], ['1910', '-45', '-44', '-51', '-40', '-35', '-38', '-35', '-37', '-36', '-38', '-54', '-66'], ['1911', '-64', '-58', '-60', '-54', '-52', '-48', '-41', '-42', '-38', '-23', '-18', '-22'], ['1912', '-26', '-12', '-35', '-20', '-22', '-26', '-44', '-55', '-51', '-57', '-38', '-43'], ['1913', '-42', '-43', '-43', '-40', '-47', '-49', '-37', '-34', '-36', '-34', '-19', '-4'], ['1914', '4', '-11', '-22', '-29', '-23', '-26', '-24', '-14', '-13', '-4', '-18', '-8'], ['1915', '-17', '0', '-10', '6', '-7', '-21', '-11', '-20', '-17', '-25', '-13', '-17'], ['1916', '-9', '-13', '-28', '-30', '-32', '-47', '-36', '-26', '-32', '-27', '-40', '-80'], ['1917', '-58', '-61', '-60', '-51', '-55', '-43', '-25', '-22', '-18', '-40', '-28', '-68'], ['1918', '-42', '-32', '-25', '-45', '-46', '-36', '-31', '-30', '-15', '-5', '-11', '-28'], ['1919', '-20', '-23', '-20', '-14', '-28', '-38', '-29', '-33', '-22', '-19', '-41', '-42'], ['1920', '-23', '-23', '-8', '-26', '-27', '-36', '-32', '-25', '-20', '-27', '-27', '-44'], ['1921', '-3', '-19', '-23', '-29', '-29', '-28', '-15', '-24', '-17', '-3', '-13', '-17'], ['1922', '-34', '-44', '-13', '-24', '-33', '-33', '-25', '-31', '-32', '-32', '-14', '-18'], ['1923', '-29', '-37', '-35', '-40', '-34', '-26', '-30', '-30', '-28', '-11', '1', '-2'], ['1924', '-22', '-24', '-7', '-31', '-18', '-23', '-26', '-34', '-31', '-34', '-20', '-41'], ['1925', '-36', '-38', '-25', '-25', '-30', '-32', '-28', '-15', '-16', '-18', '4', '9'], ['1926', '19', '6', '11', '-13', '-22', '-23', '-24', '-13', '-13', '-10', '-5', '-28'], ['1927', '-27', '-18', '-37', '-31', '-25', '-26', '-16', '-21', '-11', '-1', '-4', '-33'], ['1928', '-1', '-8', '-27', '-28', '-30', '-37', '-19', '-23', '-19', '-17', '-9', '-18'], ['1929', '-46', '-60', '-32', '-40', '-39', '-41', '-33', '-29', '-23', '-12', '-12', '-54'], ['1930', '-30', '-27', '-9', '-24', '-23', '-20', '-20', '-12', '-12', '-9', '15', '-8'], ['1931', '-10', '-24', '-8', '-21', '-20', '-7', '-3', '-3', '-6', '2', '-12', '-9'], ['1932', '14', '-18', '-19', '-6', '-18', '-29', '-24', '-23', '-12', '-10', '-27', '-26'], ['1933', '-34', '-34', '-30', '-25', '-29', '-33', '-21', '-24', '-27', '-21', '-30', '-46'], ['1934', '-24', '-4', '-33', '-30', '-11', '-15', '-10', '-11', '-16', '-8', '3', '-4'], ['1935', '-33', '15', '-13', '-36', '-30', '-27', '-22', '-22', '-21', '-8', '-28', '-20'], ['1936', '-28', '-39', '-22', '-20', '-18', '-21', '-8', '-13', '-9', '-4', '-4', '-3'], ['1937', '-13', '4', '-18', '-17', '-8', '-5', '-5', '2', '11', '8', '9', '-11'], ['1938', '1', '-2', '8', '4', '-10', '-18', '-9', '-6', '2', '11', '4', '-22'], ['1939', '-7', '-8', '-19', '-9', '-6', '-8', '-8', '-7', '-7', '-3', '6', '44'], ['1940', '-6', '13', '15', '18', '10', '8', '12', '5', '13', '8', '17', '28'], ['1941', '21', '32', '8', '17', '14', '12', '21', '14', '2', '34', '24', '21'], ['1942', '26', '2', '9', '11', '9', '4', '0', '-4', '-5', '-1', '8', '7'], ['1943', '-4', '13', '-8', '7', '4', '-6', '8', '0', '6', '22', '19', '24'], ['1944', '35', '26', '27', '20', '19', '20', '18', '17', '30', '26', '10', '2'], ['1945', '9', '-2', '7', '18', '5', '-1', '3', '27', '22', '19', '7', '-9'], ['1946', '15', '7', '1', '6', '-8', '-21', '-14', '-16', '-7', '-6', '-6', '-35'], ['1947', '-13', '-8', '7', '7', '-3', '2', '-8', '-9', '-13', '6', '2', '-16'], ['1948', '7', '-15', '-25', '-11', '1', '-4', '-11', '-12', '-11', '-7', '-12', '-23'], ['1949', '9', '-16', '-2', '-10', '-8', '-23', '-14', '-12', '-14', '-6', '-10', '-22'], ['1950', '-33', '-28', '-7', '-20', '-9', '-3', '-10', '-16', '-12', '-22', '-38', '-20'], ['1951', '-35', '-43', '-21', '-13', '0', '-2', '3', '6', '8', '5', '-2', '14'], ['1952', '13', '11', '-10', '2', '-2', '-4', '4', '4', '7', '-1', '-14', '-1'], ['1953', '8', '14', '11', '18', '9', '10', '0', '6', '4', '6', '-6', '2'], ['1954', '-28', '-13', '-15', '-17', '-21', '-17', '-19', '-17', '-10', '-4', '6', '-22'], ['1955', '14', '-17', '-34', '-21', '-21', '-12', '-12', '1', '-11', '-5', '-24', '-30'], ['1956', '-16', '-26', '-23', '-27', '-31', '-16', '-12', '-26', '-20', '-24', '-17', '-8'], ['1957', '-11', '-4', '-6', '-1', '9', '16', '1', '15', '9', '1', '6', '16'], ['1958', '39', '24', '11', '3', '7', '-7', '5', '-3', '-2', '4', '2', '0'], ['1959', '8', '9', '18', '13', '4', '2', '6', '-1', '-6', '-9', '-9', '-1'], ['1960', '-1', '16', '-34', '-14', '-7', '-4', '-3', '1', '7', '7', '-12', '18'], ['1961', '7', '19', '9', '12', '11', '11', '0', '3', '7', '0', '3', '-15'], ['1962', '7', '14', '12', '4', '-4', '4', '2', '-1', '1', '0', '7', '0'], ['1963', '-2', '20', '-14', '-6', '-4', '5', '8', '27', '20', '15', '15', '-1'], ['1964', '-8', '-13', '-23', '-31', '-25', '-2', '-3', '-21', '-29', '-31', '-21', '-30'], ['1965', '-8', '-17', '-11', '-19', '-14', '-9', '-12', '-3', '-16', '-5', '-7', '-6'], ['1966', '-17', '-1', '5', '-12', '-11', '1', '9', '-8', '-2', '-15', '-2', '-6'], ['1967', '-6', '-21', '4', '-5', '14', '-8', '1', '1', '-6', '8', '-7', '-3'], ['1968', '-25', '-15', '20', '-6', '-13', '-8', '-12', '-9', '-18', '10', '-3', '-13'], ['1969', '-11', '-14', '1', '18', '19', '5', '-2', '3', '9', '13', '13', '27'], ['1970', '9', '22', '8', '9', '-4', '-2', '-2', '-9', '11', '6', '2', '-12'], ['1971', '-2', '-20', '-17', '-8', '-5', '-19', '-11', '-3', '-4', '-6', '-5', '-9'], ['1972', '-24', '-17', '2', '0', '-4', '6', '1', '17', '2', '9', '4', '20'], ['1973', '29', '31', '27', '26', '26', '18', '10', '3', '10', '14', '6', '-6'], ['1974', '-15', '-28', '-5', '-11', '-2', '-6', '-4', '11', '-11', '-8', '-7', '-9'], ['1975', '7', '7', '13', '5', '16', '1', '-1', '-19', '-3', '-9', '-16', '-17'], ['1976', '-1', '-6', '-21', '-8', '-24', '-16', '-10', '-14', '-9', '-26', '-4', '10'], ['1977', '19', '22', '25', '28', '30', '24', '20', '16', '0', '2', '17', '2'], ['1978', '6', '14', '21', '15', '6', '-2', '3', '-17', '5', '0', '16', '10'], ['1979', '12', '-11', '18', '13', '5', '13', '2', '14', '25', '24', '28', '47'], ['1980', '30', '42', '30', '33', '36', '17', '28', '23', '19', '18', '29', '20'], ['1981', '55', '41', '49', '32', '24', '32', '34', '32', '17', '14', '24', '43'], ['1982', '10', '15', '-1', '10', '15', '5', '13', '7', '14', '13', '15', '43'], ['1983', '52', '41', '43', '31', '36', '20', '18', '33', '39', '16', '32', '17'], ['1984', '30', '17', '29', '8', '33', '5', '17', '16', '21', '16', '6', '-5'], ['1985', '22', '-6', '17', '11', '17', '18', '-1', '15', '15', '11', '8', '15'], ['1986', '28', '39', '27', '24', '24', '11', '11', '12', '0', '13', '11', '16'], ['1987', '36', '46', '16', '22', '25', '37', '45', '28', '39', '32', '25', '48'], ['1988', '58', '43', '51', '45', '43', '43', '34', '47', '41', '39', '12', '33'], ['1989', '15', '35', '36', '33', '16', '14', '34', '37', '37', '32', '20', '38'], ['1990', '41', '41', '77', '55', '46', '38', '43', '31', '29', '41', '46', '41'], ['1991', '41', '49', '35', '52', '38', '53', '48', '40', '48', '30', '31', '32'], ['1992', '44', '42', '48', '24', '32', '25', '12', '7', '0', '9', '3', '22'], ['1993', '37', '39', '36', '27', '27', '25', '28', '13', '10', '22', '6', '16'], ['1994', '27', '2', '27', '40', '28', '42', '30', '21', '29', '41', '46', '36'], ['1995', '51', '78', '45', '47', '26', '41', '48', '46', '32', '47', '45', '28'], ['1996', '25', '48', '32', '36', '27', '25', '35', '49', '25', '19', '41', '40'], ['1997', '31', '38', '53', '36', '36', '53', '35', '41', '53', '62', '65', '59'], ['1998', '60', '90', '63', '63', '69', '77', '68', '66', '42', '44', '49', '56'], ['1999', '48', '65', '34', '32', '31', '37', '37', '31', '41', '39', '38', '43'], ['2000', '23', '56', '57', '57', '36', '41', '38', '42', '40', '28', '32', '29'], ['2001', '42', '43', '55', '50', '56', '54', '60', '48', '53', '50', '69', '55'], ['2002', '75', '75', '89', '56', '63', '55', '61', '53', '62', '55', '58', '43'], ['2003', '73', '55', '57', '53', '60', '47', '53', '65', '64', '74', '52', '73'], ['2004', '58', '72', '64', '62', '38', '42', '24', '43', '50', '64', '70', '51'], ['2005', '72', '57', '69', '66', '62', '66', '63', '60', '74', '75', '71', '65'], ['2006', '56', '69', '62', '50', '46', '64', '52', '70', '61', '67', '70', '73'], ['2007', '95', '70', '69', '73', '66', '59', '60', '57', '59', '57', '54', '46'], ['2008', '23', '34', '73', '51', '47', '46', '59', '44', '62', '63', '64', '53'], ['2009', '61', '51', '52', '58', '64', '65', '69', '66', '68', '63', '76', '65'], ['2010', '73', '79', '91', '85', '73', '62', '59', '63', '59', '69', '77', '46'], ['2011', '48', '51', '62', '62', '51', '57', '71', '71', '54', '63', '56', '53'], ['2012', '45', '48', '56', '68', '75', '63', '53', '62', '72', '74', '73', '52'], ['2013', '67', '56', '65', '53', '58', '66', '57', '66', '78', '67', '78', '65'], ['2014', '73', '52', '76', '77', '85', '66', '56', '81', '88', '81', '66', '77'], ['2015', '81', '87', '90', '75', '75', '79', '71', '79', '82', '107', '103', '110'], ['2016', '115', '135', '131', '107', '91', '77', '82', '100', '88', '90', '91', '83'], ['2017', '98', '113', '113', '92', '89', '70', '82', '87', '76', '88', '86', '89'], ['2018', '77', '85', '91', '87', '81', '74', '78', '73', '76', '99', '78', '89']]
Any suggestions would be helpful. Thanks.
You are pretty far along, I am not sure how you want to store/represent the temperatures once converted to Fahrenheit but maybe this piece of code helps:
import requests
import matplotlib.pyplot as plt
import fix_yahoo_finance as yf
link = "https://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt"
f = requests.get(link)
data = f.text
lines = data.split('\n')
el = [] # creates an empty list
for each in lines[8:159]: # gets rid of unnecessary information. Prints from 1880 to 2018
tempL = each.split()
if (tempL and tempL[0] != 'Year'): # gets rid of the excess data like the title
el.append(tempL[:13])
###### What I added:
for temp_list in el:
year = int(temp_list[0]) # the first elements seems to be the year
for temp_Cel in temp_list[1:]: # all other elements are the temps
temp_Far = int(temp_Cel) * (9 / 5) + 32 # int() converts the string to a integer
print(temp_Far)

Return dictionary items if 3 fields sum to a certain number

I have a dictionary of students and their grades, and I need to return students who
Have TP01, TP02, and TP03 sum higher than 30
Here is a mock dict to work with:
{'Name': ['Colin', 'Doyon', 'Tremblay', 'Audry', 'Gamelin', 'Dagenais', 'Legault',
'Larochelle', 'Gagnon', 'Simard'],
'FirstName': ['Jonas', 'Elisabeth', 'Françcois', 'Francine', 'Geneviève',
'Robert', 'Maxime', 'Pierre', 'Emilie', 'Martine'],
'TP01': ['7', '9.5', '8', '5', '6', '8', '10', '3', '7', '8'],
'TP02': ['12', '14', '12', '13', '10', '12', '14', '11', '10', '15'],
'TP03': ['11', '12', '11', '10', '10', '13', '12', '6', '7', '14'],
'EXAMEN_INTRA': ['22', '26', '20', '22', '20', '23', '27', '15', '13', '28'],
'EXAMEN_FINAL': ['28', '20', '22', '15', '14', '22', '25', '14', '14', '27']}
I can unfortunately not use any libraries.
What I have tried:
sums = list()
for tp1, tp2, tp3 in zip(dict_etudiants['TP01'], dict_etudiants['TP02'], dict_etudiants['TP03']):
sums.append(sum((float(tp1), float(tp2), float(tp3)))) # but then it leads nowhere
The biggest problem is your data structure - you keep values in separated lists but all information about single student should be in single dictionary or list and then it would be much easier.
Something like this:
studends = [
{'Name': 'Colin', 'FirstName': 'Jonas', 'TP01': '7', 'TP02': '12', 'TP03': '11', 'EXAMEN_INTRA': '22', 'EXAMEN_FINAL': '28'},
{'Name': 'Doyon', 'FirstName': 'Elisabeth', ....},
...
]
This code get all studends with sum > 30 but it returns every student in separate list - so if you need your original structure then you will have to convert result.
dict_etudiants = {
'Name': ['Colin', 'Doyon', 'Tremblay', 'Audry', 'Gamelin', 'Dagenais', 'Legault',
'Larochelle', 'Gagnon', 'Simard'],
'FirstName': ['Jonas', 'Elisabeth', 'Françcois', 'Francine', 'Geneviève',
'Robert', 'Maxime', 'Pierre', 'Emilie', 'Martine'],
'TP01': ['7', '9.5', '8', '5', '6', '8', '10', '3', '7', '8'],
'TP02': ['12', '14', '12', '13', '10', '12', '14', '11', '10', '15'],
'TP03': ['11', '12', '11', '10', '10', '13', '12', '6', '7', '14'],
'EXAMEN_INTRA': ['22', '26', '20', '22', '20', '23', '27', '15', '13', '28'],
'EXAMEN_FINAL': ['28', '20', '22', '15', '14', '22', '25', '14', '14', '27']
}
students = list()
for tp1, tp2, tp3, name, firstname, examen_intra, examen_final in zip(dict_etudiants['TP01'], dict_etudiants['TP02'], dict_etudiants['TP03'],
dict_etudiants['Name'], dict_etudiants['FirstName'], dict_etudiants['EXAMEN_INTRA'], dict_etudiants['EXAMEN_FINAL']):
if sum((float(tp1), float(tp2), float(tp3))) > 30:
students.append([name, firstname, tp1, tp2, tp3, examen_intra, examen_final])
print(students)
Result
[
['Doyon', 'Elisabeth', '9.5', '14', '12', '26', '20'],
['Tremblay', 'Françcois', '8', '12', '11', '20', '22'],
['Dagenais', 'Robert', '8', '12', '13', '23', '22'],
['Legault', 'Maxime', '10', '14', '12', '27', '25'],
['Simard', 'Martine', '8', '15', '14', '28', '27']
]
EDIT: I released that you could convert it to pandas.DataFrame and then it is easy to search students.
result = df[ df['TP01'] + df['TP02'] + df['TP03'] > 30 ]
Full code
dict_etudiants = {
'Name': ['Colin', 'Doyon', 'Tremblay', 'Audry', 'Gamelin', 'Dagenais', 'Legault',
'Larochelle', 'Gagnon', 'Simard'],
'FirstName': ['Jonas', 'Elisabeth', 'Françcois', 'Francine', 'Geneviève',
'Robert', 'Maxime', 'Pierre', 'Emilie', 'Martine'],
'TP01': ['7', '9.5', '8', '5', '6', '8', '10', '3', '7', '8'],
'TP02': ['12', '14', '12', '13', '10', '12', '14', '11', '10', '15'],
'TP03': ['11', '12', '11', '10', '10', '13', '12', '6', '7', '14'],
'EXAMEN_INTRA': ['22', '26', '20', '22', '20', '23', '27', '15', '13', '28'],
'EXAMEN_FINAL': ['28', '20', '22', '15', '14', '22', '25', '14', '14', '27']
}
import pandas as pd
import pprint
# create DateFrame
df = pd.DataFrame(dict_etudiants)
# convert strings to float
df['TP01'] = df['TP01'].astype(float)
df['TP02'] = df['TP02'].astype(float)
df['TP03'] = df['TP03'].astype(float)
# search students
result = df[ df['TP01'] + df['TP02'] + df['TP03'] > 30 ]
# convert back to dictionary
dict_etudiants_selected = result.to_dict(orient='list')
print('\n--- DataFrame ---\n')
print(result)
print('\n--- dict ---\n')
pprint.pprint(dict_etudiants_selected)
Result
--- DataFrame ---
Name FirstName TP01 TP02 TP03 EXAMEN_INTRA EXAMEN_FINAL
1 Doyon Elisabeth 9.5 14.0 12.0 26 20
2 Tremblay Françcois 8.0 12.0 11.0 20 22
5 Dagenais Robert 8.0 12.0 13.0 23 22
6 Legault Maxime 10.0 14.0 12.0 27 25
9 Simard Martine 8.0 15.0 14.0 28 27
--- dict ---
{'EXAMEN_FINAL': ['20', '22', '22', '25', '27'],
'EXAMEN_INTRA': ['26', '20', '23', '27', '28'],
'FirstName': ['Elisabeth', 'Françcois', 'Robert', 'Maxime', 'Martine'],
'Name': ['Doyon', 'Tremblay', 'Dagenais', 'Legault', 'Simard'],
'TP01': [9.5, 8.0, 8.0, 10.0, 8.0],
'TP02': [14.0, 12.0, 12.0, 14.0, 15.0],
'TP03': [12.0, 11.0, 13.0, 12.0, 14.0]}

TypeError: unsupported operand type(s) for /: 'float' and 'str' - Python/Matplotlib [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the two following lists:
list1=['21', '28', '28', '18', '17', '17', '18', '16', '20', '21', '22', '22', '20', '17', '23', '21', '20', '21', '21', '22', '22', '22', '21', '22', '21', '20', '21', '20', '22', '21', '24', '24', '23', '23', '23', '22', '22', '22', '23', '24', '22', '23', '24', '20', '22', '23', '24', '23', '24', '22', '25', '26', '22', '21', '21', '21', '21', '20', '21', '23', '23', '23', '24', '21', '26', '26', '27', '26', '26', '29', '27', '26', '25', '27', '27', '26', '26', '24', '24', '23', '26', '24', '26', '29', '29', '28', '27', '27', '26', '26', '26', '28', '25', '26', '26', '24', '25', '25', '26', '25', '25', '25', '26', '31', '25', '24', '24', '24', '24', '24', '25', '24', '22', '26', '27', '26', '27', '28', '25', '28', '27', '28', '27', '29', '28', '28', '29', '28', '28', '25', '27', '27', '27', '27', '27', '29', '31', '30', '28', '28', '27', '28', '28', '27', '26', '28', '27', '25', '27', '25', '27', '26', '26', '27', '28', '32', '28', '26', '27', '26', '25', '25', '26', '25', '26', '25', '27', '26', '26', '25', '28', '28', '29', '29', '31', '34', '33', '31', '32', '32', '30', '32', '32', '31', '32', '33', '35', '36', '37', '35', '35', '34', '33', '32', '32', '34', '32', '32', '32', '31', '32', '31', '33', '31', '33', '33', '34', '37', '32', '32', '32', '32', '32', '30', '34', '35', '33', '35', '34', '32', '36', '38', '38', '38', '37', '37', '38', '39', '37', '38', '38', '39', '39', '39', '38', '39', '38', '41', '39', '43', '44', '44', '45', '44', '42', '42', '44', '43', '41', '43', '43', '43', '40', '41', '39', '41', '39', '40', '40', '45', '42', '41', '40', '40', '41', '40', '42', '42', '38', '42', '44', '45', '45', '45', '44', '46', '48', '47', '50', '51', '50', '51', '49', '50', '48', '49', '50', '51', '50', '52', '53', '56', '57', '55', '54', '53', '55', '53', '54', '51', '53', '52', '51', '49', '48', '48', '49', '50', '47', '48', '51', '53', '53', '50', '47', '47', '48', '47', '48', '47', '49', '48', '52', '60', '58', '55', '100', '61', '55', '55', '54', '54', '54', '55', '55', '54', '53', '54', '53', '55', '56', '54', '55', '56', '58', '56', '56', '56', '55', '54', '55', '56', '56', '56', '56', '53', '54', '54', '54', '54', '52', '52', '54', '57', '58', '55', '54', '52', '52', '52', '54', '55', '54', '53', '53', '56', '57', '56', '56', '58', '55', '61', '59', '60', '61', '60', '64', '61', '62', '61', '60', '62', '63', '63', '65', '67', '69', '71', '68', '69', '66', '68', '70', '69', '69', '70', '69', '67', '68', '66', '65', '66', '63', '63', '64', '66', '72', '68', '65', '62', '64', '61', '63', '60', '59', '62', '63', '64', '65', '63', '61', '64', '63', '61', '61', '63', '64', '64', '63', '62', '65', '64', '64', '64', '64', '68', '66', '68', '69', '72', '70', '68', '66', '68', '65', '71', '69', '62', '63', '64', '61', '63', '64', '64', '64', '62', '60', '62', '70', '66', '65', '62', '62', '62', '62', '60', '64', '64', '64', '66', '66', '63', '69', '68', '66', '67', '69', '68', '67', '67', '68', '66', '68', '67', '68', '70', '71', '69', '70', '70', '75', '75', '73', '69', '71', '69', '69', '69', '73', '71', '71', '69', '67', '67', '65', '68', '69', '64', '66', '65', '70', '66', '66', '64', '63', '65', '64', '65', '66', '65', '65', '66', '68', '65', '65', '69', '66', '68', '65', '70', '69', '71', '69', '71', '72', '69', '68', '67', '68', '71', '72', '74', '76', '72', '71', '72', '70', '69', '67', '70', '70', '66', '66', '68', '67', '66', '67', '65', '74', '68', '67', '69', '69', '68', '69', '67', '67', '66', '64', '63', '66', '66', '66', '66', '66', '64', '62', '66', '66', '65', '66', '68', '70', '69', '68', '67', '67', '67', '64', '67', '66', '68', '66', '69', '68', '69', '69', '65', '66', '69', '68', '69', '66', '65', '65', '66', '66', '65', '66']
list2=['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '4', '3', '3', '2', '2', '3', '2', '2', '3', '3', '2', '2', '2', '2', '2', '2', '3', '2', '2', '2', '3', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '2', '2', '2', '2', '2', '2', '2', '3', '2', '2', '3', '3', '2', '3', '4', '4', '3', '5', '4', '5', '3', '4', '4', '5', '4', '3', '4', '3', '5', '4', '4', '3', '3', '5', '4', '4', '3', '4', '4', '3', '3', '3', '3', '3', '4', '5', '4', '6', '5', '5', '6', '5', '5', '6', '6', '6', '6', '6', '6', '5', '6', '8', '9', '9', '6', '8', '7', '6', '7', '8', '11', '10', '8', '12', '10', '9', '9', '10', '10', '8', '9', '10', '11', '12', '13', '11', '12', '11', '9', '10', '14', '9', '11', '8', '8', '9', '9', '8', '9', '10', '7', '8', '8', '8', '9', '10', '9', '9', '11', '12', '12', '11', '12', '12', '12', '12', '11', '9', '11', '10', '10', '9', '11', '10', '10', '9', '9', '9', '10', '10', '10', '9', '9', '7', '8', '7', '8', '8', '11', '11', '10', '13', '11', '11', '12', '10', '11', '9', '17', '11', '12', '13', '15', '19', '25', '23', '27', '28', '29', '36', '36', '38', '33', '36', '30', '35', '35', '35', '30', '36', '33', '34', '34', '34', '33', '32', '36', '33', '43', '37', '33', '37', '35', '35', '36', '40', '34', '34', '30', '29', '32', '33', '34', '31', '31', '34', '36', '29', '25', '23', '21', '22', '23', '23', '22', '24', '23', '23', '26', '31', '28', '31', '35', '35', '32', '33', '27', '30', '32', '32', '32', '31', '34', '34', '43', '48', '53', '62', '63', '70', '62', '65', '67', '63', '60', '57', '62', '51', '54', '56', '58', '56', '58', '60', '71', '61', '55', '57', '57', '59', '50', '50', '44', '40', '37', '37', '36', '42', '40', '39', '40', '35', '38', '38', '36', '45', '47', '100', '69', '51', '47', '46', '48', '45', '41', '44', '42', '43', '44', '37', '41', '42', '42', '38', '36', '38', '40', '34', '34', '33', '36', '38', '36', '35', '36', '30', '34', '34', '29', '28', '26', '22', '19', '19', '47', '36', '24', '27', '27', '28', '27', '27', '21', '18', '18', '16', '14', '14', '14', '13', '12', '13', '15', '12', '12', '13', '11', '11', '10', '13', '11', '10', '11', '11', '10', '10', '10', '8', '9', '9', '8', '7', '7', '7', '7', '6', '7', '7', '8', '6', '7', '6', '6', '6', '5', '5', '6', '5', '5', '5', '6', '6', '6', '5', '6', '6', '6', '6', '6', '6', '6', '5', '6', '6', '6', '6', '5', '6', '7', '6', '6', '6', '6', '6', '6', '5', '6', '6', '6', '5', '5', '5', '6', '5', '5', '5', '5', '5', '5', '5', '6', '5', '5', '4', '4', '5', '4', '4', '4', '4', '4', '4', '4', '5', '4', '5', '5', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '4', '5', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '3', '3', '4', '4', '4', '3', '3', '3', '3', '3', '3', '4', '3', '3', '3', '4', '3', '3', '3', '3', '4', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '3', '3', '2', '3', '3', '3', '3', '3', '3', '3', '3', '2']
I'm passing them through the following function:
def line_plot(list1,list2,datelist,labellist1="list1",labellist2="list2"):
"""
Plots a chart based on two different lists
:param list1: list
:param list2: list
:param datelist: a list of dates
:return: a chart with both lists
"""
import matplotlib.pyplot as plt
norm_list1=[float(i)/max(list1) for i in list1]
norm_list2=[float(i)/max(list2) for i in list2]
plt.plot(datelist,norm_list1,label=labellist1)
plt.plot(datelist,norm_list2,label=labellist2)
plt.xlabel("Date")
plt.legend()
plt.show()
I'm getting the following error message:
Traceback (most recent call last):
File "/Users/santanna_santanna/PycharmProjects/APIGTrends/TrendVesting.py", line 670, in <module>
line_plot(list_of_lists[0],list_of_lists[1],datelist)
File "/Users/santanna_santanna/PycharmProjects/APIGTrends/TrendVesting.py", line 587, in line_plot
norm_list1=[float(i)/max(list1) for i in list1]
TypeError: unsupported operand type(s) for /: 'float' and 'str'
Any ideas on what am I doing wrong?
You try to divide an integer by a string.
norm_list1=[float(i)/max(list1) for i in list1]
norm_list2=[float(i)/max(list2) for i in list2]
max(list1) will give the heighest string in the list, since your lists are made of strings. Why do you have strings in the list and not integers? Convert your list to int or float or use the code below
To solve the error and keep your strings (which is probably not what you want):
norm_list1=[float(i)/float(max(list1)) for i in list1]
norm_list2=[float(i)/float(max(list2)) for i in list2]
To just convert your lists to int or float and solve the error (probably what you want):
newlist1 = [float(i) for i in list1]
newlist2 = [float(i) for i in list2]
And use the new lists instead.
max from list of string integers returns also string, so you are trying to divide float and string.
>>> max(['333', '22222'])
'333'
Why? answer
>>> 2.0 / '3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'float' and 'str'

Categories

Resources