Transpose nested list in python - python

I like to move each item in this list to another nested list could someone help me?
a = [['AAA', '1', '1', '10', '92'], ['BBB', '262', '56', '238', '142'], ['CCC', '86', '84', '149', '30'], ['DDD', '48', '362', '205', '237'], ['EEE', '8', '33', '96', '336'], ['FFF', '39', '82', '89', '140'], ['GGG', '170', '296', '223', '210'], ['HHH', '16', '40', '65', '50'], ['III', '4', '3', '5', '2']]
On the end I will make list like this:
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF'.....],
['1', '262', '86', '48', '8', '39', ...],
['1', '56', '84', '362', '33', '82', ...],
['10', '238', '149', '205', '96', '89', ...],
...
...]

Use zip with * and map:
>>> map(list, zip(*a))
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]
Note that map returns a map object in Python 3, so there you would need list(map(list, zip(*a)))
Using a list comprehension with zip(*...), this would work as is in both Python 2 and 3.
[list(x) for x in zip(*a)]
NumPy way:
>>> import numpy as np
>>> np.array(a).T.tolist()
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]

Through list comprehension:
[[x[i] for x in mylist] for i in range(len(mylist[0]))]

You can also use
a= np.array(a).transpose().tolist()

You could also do:
row1 = [1,2,3]
row2 = [4,5,6]
row3 = [7,8,9]
matrix = [row1, row2, row3]
trmatrix = [[row[0] for row in matrix],[row[1] for row in matrix], [row[2] for row in matrix]]

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']

How to bubble sort a 2D array with python

I have this list that's a summary of a few NHL player stats in 2018. I want to sort them by pts which is the 7th value using bubble. I am aware of the built-in sort function on python but I would rather use bubble sort or even quicksort for that matter. Can anyone help out?
[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
This is what I did so far:
def sortByPoints(stats):
lengthOfstats = len(stats) - 1
for i in range(lengthOfstats):
for j in range(lengthOfstats - i):
if stats[j] < stats[j + 1]:
stats[j], stats[j + 1] = stats[j + 1], stats[j]
return stats
print(sortByPoints(readStatsFromFile()))
Create bubble sort that can sort nested-arrays based upon an index of sub-array
Modification of BubbleSort
def bubbleSort(arr, ind = 6):
"""Bubble sort arr based upon subelement ind (default of index 6)
which is 7th element of sub-array since 0 based indexing"""
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if int(arr[j][ind]) > int(arr[j+1][ind]) :
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
Test
arr = [['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
import pprint
print('pre-sorted')
pprint.pprint(arr)
print('sorted')
pprint.pprint(bubbleSort(arr))
Output
pre-sorted
[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'],
['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'],
['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'],
['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'],
['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'],
['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
sorted
[['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'],
['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'],
['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'],
['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15'],
['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'],
['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88']]

Searching for an element in list of lists

I am trying to search for an element in a list of lists, and return the second element of that list.
I am new to programming and I want to search for a word in a list of lists that look like this:
list = [['592', 'Frillish', 'Water', 'Ghost', '335', '55', '40', '50', '65', '85', '40', '5', 'False']
['593', 'Jellicent', 'Water', 'Ghost', '480', '100', '60', '70', '85', '105', '60', '5', 'False']
['594', 'Alomomola', 'Water', '', '470', '165', '75', '80', '40', '45', '65', '5', 'False']
['595', 'Joltik', 'Bug', 'Electric', '319', '50', '47', '50', '57', '50', '65', '5', 'False']]
What I have now is something like :
def search(word):
result_of_search = []
for i in list:
if word in i:
result_of_search.append.......
This is the part where i get stuck. I want to return the second element in the list , if the word exist in the list.
This is how you should do:
[x[1] for x in lst if word in x]
Note that I have renamed your list to lst as list is a built-in.
In a function:
def search(word):
return [x[1] for x in lst if word in x]
Read about list comprehensions.
I tried like below:
l = [['592', 'Frillish', 'Water', 'Ghost', '335', '55', '40', '50', '65', '85', '40', '5', 'False'],
['593', 'Jellicent', 'Water', 'Ghost', '480', '100', '60', '70', '85', '105', '60', '5', 'False'],
['594', 'Alomomola', 'Water', '', '470', '165', '75', '80', '40', '45', '65', '5', 'False'],
['595', 'Joltik', 'Bug', 'Electric', '319', '50', '47', '50', '57', '50', '65', '5', 'False']]
import itertools
def search(l,word):
merged = list(itertools.chain(*l))
if merged.count(word)== 0:
print("not present")
else:
print(word,"--repeated times:", merged.count(word))
search(l,"apple")
ans: not present
search(l,"Water")
ans: Water --repeated times: 3

How do I avoid this error for this piece of code-TypeError: 'NoneType' object is not subscriptable

def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
print(open_csv()[1])
This is the output for open_csv():
['Name', 'Gender', 'Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5', 'Test 6']
['Aisha', 'F', '0', '33', '67', '27', '12', '14']
['Alex', 'M', '12', '90', '34', '56', '93', '39']
['Bala', 'M', '13', '25', '58', '17', '49', '29']
['Denise', 'F', '13', '93', '84', '53', '65', '62']
['Farhan', 'M', '15', '5', '10', '62', '34', '11']
['Gopi', 'M', '21', '61', '39', '32', '91', '32']
['Irfan', 'M', '26', '36', '3', '95', '36', '39']
['Jun Ming', 'M', '29', '86', '77', '6', '91', '61']
['Lily', 'F', '30', '34', '46', '96', '100', '44']
['Mei Ling', 'F', '39', '58', '9', '61', '32', '46']
['Muthu', 'M', '39', '60', '13', '69', '55', '100']
['Nurul', 'F', '50', '35', '4', '27', '11', '97']
['Priya', 'F', '50', '25', '47', '15', '35', '86']
['Siti', 'F', '58', '71', '13', '19', '58', '30']
['Elisa', 'F', '59', '22', '73', '52', '77', '49']
['Dennis', 'M', '65', '94', '83', '67', '37', '22']
['Harry', 'M', '74', '75', '76', '82', '57', '1']
['Gary', 'M', '90', '12', '70', '86', '50', '59']
['Terry', 'M', '93', '84', '26', '99', '90', '72']
['Corinne', 'F', '100', '17', '88', '14', '33', '9']
When I run this code, I get TypeError:"NoneType" object is not subscriptable. Why is this so and how do I fix it?
If you want to print the second row in your CSV file, you could modify your function as follows:
def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r', newline='') as f:
return list(csv.reader(f))
print(open_csv()[1])
This will return all the data as a list of rows, by adding [1] you will be displaying the second row (with [0] being the first row). The reason you were getting the error is that your code did not have a return statement in the function. The default is for Python to return None. So in effect your code was doing:
print(None[0])
Don't forget to add newline='' when using it for a csv.reader()
Turn this function into a generator.
def open_csv():
...
for row in reader:
print(row) // change this to yield(row)
list(open_csv())[0]

Numpy Meshgrid Error: Unsupported Operand Types

Currently playing around with the 3d plotting capabilities of matplotlib. I have a data file filled up with some... well... data. Let's say it has angle data, order data, and intensity data. I want to make a contour plot of order on the x-axis, angle on the y-axis, and intensity as the color. I fill up arrays orders and angles with their values, and ints is a two-dimensional array of dimensions len(orders)xlen(angles). When I do this:
orders = np.array(orders)
angles = np.array(angles)
Orders, Angles = np.meshgrid(orders, angles)
ints = np.array(ints)
The error:
/anaconda/lib/python2.7/site-packages/numpy/lib/function_base.pyc in meshgrid(*xi, **kwargs)
3377 if copy_:
3378 mult_fact = np.ones(shape, dtype=int)
-> 3379 return [x * mult_fact for x in output]
3380 else:
3381 return np.broadcast_arrays(*output)
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.ndarray'
Pretty confused, as I do believe I'm using the meshgrid function properly. Any idea what's going on here? On a related note, if I get this working properly, will:
ax.contourf(Angles, Orders, ints)
produce the result I'm looking for (where ax is my Axes3D object)?
EDIT: As requested, here is some sample data. Printing angles provides:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '220', '221', '222', '223', '224', '225', '226', '227', '228', '229', '230', '231', '232', '233', '234', '235', '236', '237', '238', '239', '240', '241', '242', '243', '244', '245', '246', '247', '248', '249', '250', '251', '252', '253', '254', '255', '256', '257', '258', '259', '260', '261', '262', '263', '264', '265', '266', '267', '268', '269', '270', '271', '272', '273', '274', '275', '276', '277', '278', '279', '280', '281', '282', '283', '284', '285', '286', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '302', '303', '304', '305', '306', '307', '308', '309', '310', '311', '312', '313', '314', '315', '316', '317', '318', '319', '320', '321', '322', '323', '324', '325', '326', '327', '328', '329', '330', '331', '332', '333', '334', '335', '336', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '357', '358', '359', '360']
And printing orders:
['6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']
so I don't think it's a problem with converting these arrays to ndarray's
double edit: Welp, I'm stupid -- as simple as converting the data I'm reading from my files to floats before putting them in their arrays.

Categories

Resources