Related
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']
i am extremely new to python. i have googled but i haven't found what i need for hours.
i'm very confused on why my program is like this and would very much appreciate pointers. so this is my program:
number = int(input("Enter number: "))
mylist = []
if number > 1:
for num in range(1,number + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
mylist.append(str(num))
print(mylist)
else:
print("no prime number")
so e.g if i insert 50 i wanted it to have an output of something like:
['2', '3', '5', '7', '11', '13', '17', '19',
'23', '29', '31', '37', '41', '43', '47']
but instead what i got was this chaos
['2']
['2', '3']
['2', '3', '5']
['2', '3', '5', '7']
['2', '3', '5', '7', '11']
['2', '3', '5', '7', '11', '13']
['2', '3', '5', '7', '11', '13', '17']
['2', '3', '5', '7', '11', '13', '17', '19']
['2', '3', '5', '7', '11', '13', '17', '19', '23']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41', '43']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41', '43', '47']
Your print statement is indented incorrectly:
number = int(input("Enter number: "))
mylist = []
if number > 1:
for num in range(1,number + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
mylist.append(str(num))
print(mylist)
else:
print("no prime number")
You can group them into sublists of 8 for the purpose of printing. Just make your mylist as usual by appending the values then after you have processed all the values you can print sublists of 8
number = int(input("Enter number: "))
mylist = []
if number > 1:
for num in range(1,number + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
mylist.append(str(num))
print(*[mylist[i:i+8] for i in range(0, len(mylist), 8)], sep="\n")
else:
print("no prime number")
OUTPUT
['2', '3', '5', '7', '11', '13', '17', '19']
['23', '29', '31', '37', '41', '43', '47']
I'm trying to do some webscraping to download all the results of euromillions, stuck with errors now. I'm using jupyter and python 3 with the modules specified. With just one link the code worked just fine but now I added a loop and some modifications and rip xD
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
years = list(range(2004,2018))
for year in years:
my_urls = ('https://www.euro-millions.com/pt/arquivo-de-resultados-' + str(year),)
my_url = my_urls[0]
for my_url in my_urls:
Client = uReq(my_url)
html = Client.read
Client.close()
euro = soup(html, "html")
containers = euro.findAll("div",{"class":"archives"})
print(containers)
container = containers[0]
for container in containers:
data = container.a["href"].replace('/pt/resultados/','') #Usamos os [] como num dicionario, .strip tbm retira o lixo sometimes
bolasN = container.ul.findAll("li",{"class":"ball"})
bolasS = container.ul.findAll("li",{"class":"lucky-star"})
bola1 = bolasN[0].text
bola2 = bolasN[1].text
bola3 = bolasN[2].text
bola4 = bolasN[3].text
bola5 = bolasN[4].text
star1 = bolasS[0].text
star2 = bolasS[1].text
TUDO = [data, bola1, bola2, bola3, bola4, bola5, star1, star2]
print(TUDO)
TRACEBACK:
TypeError Traceback (most recent call last)
<ipython-input-31-b11e2044b5ea> in <module>
12 html = Client.read
13 Client.close()
---> 14 euro = soup(html, "html")
15 containers = euro.findAll("div",{"class":"archives"})
16 print(containers)
/usr/local/lib/python3.5/dist-packages/bs4/__init__.py in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, **kwargs)
244 if hasattr(markup, 'read'): # It's a file-type object.
245 markup = markup.read()
--> 246 elif len(markup) <= 256 and (
247 (isinstance(markup, bytes) and not b'<' in markup)
248 or (isinstance(markup, str) and not '<' in markup)
TypeError: object of type 'method' has no len()
The markup was wrong, it should be 'html.parser' or 'lxml'
import requests
from bs4 import BeautifulSoup as soup
years = list(range(2004,2018))
for year in years:
my_urls = ('https://www.euro-millions.com/pt/arquivo-de-resultados-' + str(year),)
my_url = my_urls[0]
for my_url in my_urls:
Client = requests.get(my_url)
euro = soup(Client.content, "html.parser")
containers = euro.findAll("div",{"class":"archives"})
#print(containers)
container = containers[0]
for container in containers:
data = container.a["href"].replace('/pt/resultados/','') #Usamos os [] como num dicionario, .strip tbm retira o lixo sometimes
bolasN = container.ul.findAll("li",{"class":"ball"})
bolasS = container.ul.findAll("li",{"class":"lucky-star"})
bola1 = bolasN[0].text
bola2 = bolasN[1].text
bola3 = bolasN[2].text
bola4 = bolasN[3].text
bola5 = bolasN[4].text
star1 = bolasS[0].text
star2 = bolasS[1].text
TUDO = [data, bola1, bola2, bola3, bola4, bola5, star1, star2]
print(TUDO)
Output:
['29-12-2017', '4', '8', '22', '23', '48', '1', '12']
['26-12-2017', '4', '17', '30', '43', '44', '2', '10']
['22-12-2017', '5', '24', '30', '31', '43', '3', '6']
['19-12-2017', '8', '15', '30', '38', '46', '4', '7']
['15-12-2017', '25', '30', '31', '42', '50', '2', '11']
['12-12-2017', '20', '37', '39', '44', '50', '4', '8']
['08-12-2017', '4', '22', '30', '32', '34', '3', '4']
['05-12-2017', '11', '36', '43', '44', '48', '2', '7']
['01-12-2017', '5', '24', '29', '35', '46', '11', '12']
['28-11-2017', '1', '6', '12', '18', '42', '2', '7']
['24-11-2017', '19', '24', '28', '30', '50', '3', '10']
['21-11-2017', '2', '10', '14', '28', '31', '5', '7']
['17-11-2017', '20', '26', '35', '36', '42', '5', '12']
['14-11-2017', '14', '16', '39', '40', '41', '8', '10']
['10-11-2017', '13', '22', '29', '36', '37', '1', '9']
['07-11-2017', '7', '19', '20', '37', '41', '2', '12']
['03-11-2017', '5', '12', '17', '33', '41', '4', '9']
['31-10-2017', '1', '12', '36', '43', '46', '3', '5']
['27-10-2017', '3', '16', '23', '32', '39', '1', '4']
['24-10-2017', '9', '11', '13', '27', '33', '7', '10']
['20-10-2017', '4', '17', '23', '27', '30', '3', '8']
['17-10-2017', '13', '17', '19', '26', '36', '2', '3']
['13-10-2017', '23', '29', '37', '45', '50', '5', '11']
['10-10-2017', '4', '21', '34', '36', '37', '3', '6']
['06-10-2017', '1', '9', '15', '19', '25', '1', '7']
['03-10-2017', '6', '24', '32', '48', '50', '1', '5']
['29-09-2017', '7', '18', '19', '32', '48', '3', '7']
['26-09-2017', '1', '29', '40', '41', '48', '6', '12']
['22-09-2017', '6', '11', '31', '39', '42', '1', '3']
['19-09-2017', '1', '8', '21', '30', '45', '2', '3']
['15-09-2017', '13', '18', '37', '44', '49', '9', '12']
['12-09-2017', '10', '17', '27', '29', '35', '4', '11']
['08-09-2017', '9', '24', '42', '47', '49', '1', '5']
['05-09-2017', '6', '9', '18', '28', '29', '1', '9']
['01-09-2017', '3', '7', '8', '14', '49', '5', '8']
['29-08-2017', '4', '12', '15', '32', '38', '1', '5']
['25-08-2017', '1', '5', '7', '15', '47', '9', '12']
['22-08-2017', '3', '10', '12', '17', '27', '3', '5']
['18-08-2017', '2', '24', '39', '42', '45', '2', '8']
['15-08-2017', '10', '14', '30', '35', '46', '4', '10']
['11-08-2017', '18', '28', '39', '46', '48', '5', '12']
['08-08-2017', '15', '25', '26', '40', '41', '4', '5']
['04-08-2017', '29', '30', '36', '40', '41', '2', '9']
['01-08-2017', '14', '21', '24', '29', '30', '8', '10']
['28-07-2017', '5', '9', '29', '31', '41', '2', '4']
['25-07-2017', '12', '14', '43', '44', '48', '2', '11']
['21-07-2017', '1', '8', '9', '26', '49', '5', '9']
['18-07-2017', '1', '25', '27', '41', '45', '5', '7']
['14-07-2017', '11', '14', '20', '21', '47', '7', '10']
['11-07-2017', '14', '22', '26', '42', '50', '8', '10']
['07-07-2017', '11', '20', '35', '37', '45', '3', '6']
['04-07-2017', '10', '22', '25', '37', '49', '5', '8']
['30-06-2017', '17', '35', '39', '47', '50', '6', '8']
['27-06-2017', '9', '17', '21', '28', '45', '1', '3']
['23-06-2017', '3', '4', '21', '31', '38', '3', '7']
['20-06-2017', '11', '18', '26', '43', '44', '8', '10']
['16-06-2017', '15', '17', '38', '41', '42', '9', '12']
['13-06-2017', '3', '12', '22', '27', '49', '4', '11']
['09-06-2017', '9', '20', '27', '39', '43', '10', '11']
['06-06-2017', '20', '22', '25', '37', '40', '3', '7']
['02-06-2017', '8', '10', '24', '33', '42', '3', '9']
['30-05-2017', '7', '12', '27', '38', '48', '6', '9']
['26-05-2017', '5', '7', '26', '36', '39', '2', '10']
['23-05-2017', '8', '15', '25', '27', '42', '1', '4']
['19-05-2017', '9', '11', '12', '19', '30', '4', '9']
['16-05-2017', '8', '11', '15', '20', '30', '3', '8']
['12-05-2017', '2', '20', '28', '29', '44', '3', '9']
['09-05-2017', '8', '12', '16', '22', '26', '6', '7']
['05-05-2017', '3', '7', '30', '35', '43', '1', '3']
['02-05-2017', '6', '19', '23', '25', '27', '11', '12']
['28-04-2017', '14', '20', '25', '30', '39', '2', '8']
['25-04-2017', '9', '11', '19', '32', '43', '3', '9']
['21-04-2017', '2', '13', '16', '22', '49', '4', '5']
['18-04-2017', '17', '22', '31', '38', '45', '5', '12']
['14-04-2017', '4', '14', '20', '23', '33', '6', '10']
['11-04-2017', '5', '21', '22', '31', '49', '2', '8']
['07-04-2017', '2', '10', '19', '35', '50', '6', '7']
['04-04-2017', '1', '9', '24', '33', '34', '2', '6']
['31-03-2017', '17', '24', '26', '28', '45', '4', '12']
['28-03-2017', '9', '13', '31', '33', '46', '6', '10']
['24-03-2017', '2', '17', '21', '27', '34', '5', '9']
['21-03-2017', '1', '20', '23', '44', '47', '4', '11']
['17-03-2017', '6', '10', '19', '29', '36', '3', '9']
['14-03-2017', '3', '5', '21', '36', '44', '3', '6']
['10-03-2017', '31', '36', '38', '47', '49', '8', '11']
['07-03-2017', '6', '37', '41', '48', '50', '4', '5']
['03-03-2017', '2', '11', '29', '30', '47', '1', '12']
['28-02-2017', '10', '20', '31', '35', '42', '2', '12']
['24-02-2017', '2', '4', '13', '22', '43', '8', '9']
['21-02-2017', '13', '19', '41', '45', '49', '3', '4']
['17-02-2017', '19', '25', '33', '36', '48', '2', '9']
['14-02-2017', '2', '10', '24', '40', '44', '3', '10']
['10-02-2017', '7', '21', '26', '35', '43', '2', '9']
['07-02-2017', '4', '10', '31', '38', '44', '8', '10']
['03-02-2017', '3', '4', '15', '46', '50', '5', '9']
['31-01-2017', '3', '4', '17', '23', '44', '6', '9']
['27-01-2017', '17', '20', '28', '45', '48', '5', '9']
['24-01-2017', '1', '5', '7', '17', '23', '3', '8']
['20-01-2017', '10', '17', '27', '31', '49', '3', '5']
['17-01-2017', '4', '16', '25', '43', '47', '2', '10']
['13-01-2017', '3', '7', '16', '26', '50', '4', '7']
['10-01-2017', '2', '11', '29', '35', '44', '4', '9']
['06-01-2017', '10', '14', '18', '21', '49', '9', '11']
['03-01-2017', '19', '23', '27', '34', '49', '1', '11']
try passing HTML text directly
soup = BeautifulSoup(html.text)
I am trying to create an array using two lists, one of which has a list for each element. The problem is that in the first case I manage to do what I want, using np.column_stack but in the second case, although my initial lists look similar (in structure), my list of lists enters the array flattened (which is not what I need.
I am attaching two examples to replicate, on the first case, I manage to get an array, where each line has a string as first element, and a list as a second, while in the second case, I get 4 columns (the list is flattened) with no obvious reason.
Example 1
temp_list_column1=['St. Raphael',
'Goppingen',
'HSG Wetzlar',
'Huttenberg',
'Kiel',
'Stuttgart',
'Izvidac',
'Viborg W',
'Silkeborg-Voel W',
'Bjerringbro W',
'Lyngby W',
'Most W',
'Ostrava W',
'Presov W',
'Slavia Prague W',
'Dicken',
'Elbflorenz',
'Lubeck-Schwartau',
'HK Ogre/Miandum',
'Stal Mielec',
'MKS Perla Lublin W',
'Koscierzyna W',
'CS Madeira W',
'CSM Focsani',
'CSM Bucuresti',
'Constanta',
'Iasi',
'Suceava',
'Timisoara',
'Saratov',
'Alisa Ufa W',
'Pozarevac',
'Nove Zamky',
'Aranas',
'Ricoh',
'H 65 Hoor W',
'Lugi W',
'Strands W']
temp_list_column2=[['32', '16', '16'],
['32', '16', '16'],
['27', '13', '14'],
['23', '9', '14'],
['29', '14', '15'],
['24', '17', '7'],
['30', '15', '15'],
['26', '12', '14'],
['27', '13', '14'],
['26'],
['18', '9', '9'],
['34', '15', '19'],
['30', '13', '17'],
['31', '13', '18'],
['27', '10', '17'],
['28', '14', '14'],
['24', '14', '10'],
['28', '12', '16'],
['28', '9', '19'],
['22', '13', '9'],
['30', '14', '16'],
['22', '14', '8'],
['17', '8', '9'],
['26'],
['41', '21', '20'],
['36', '18', '18'],
['10'],
['25', '12', '13'],
['27', '16', '11'],
['31', '15', '16'],
['25', '15', '10'],
['24', '8', '16'],
['28', '14', '14'],
['24', '13', '11'],
['26', '14', '12'],
['33', '17', '16'],
['26', '12', '14'],
['17', '12', '5']]
import numpy as np
temp_array = np.column_stack((temp_list_column1,temp_list_column2))
output
array([['St. Raphael', ['32', '16', '16']],
['Goppingen', ['32', '16', '16']],
['HSG Wetzlar', ['27', '13', '14']],
['Huttenberg', ['23', '9', '14']],
['Kiel', ['29', '14', '15']],
['Stuttgart', ['24', '17', '7']],
['Izvidac', ['30', '15', '15']],
['Viborg W', ['26', '12', '14']],
['Silkeborg-Voel W', ['27', '13', '14']],
['Bjerringbro W', ['26']],
['Lyngby W', ['18', '9', '9']],
['Most W', ['34', '15', '19']],
['Ostrava W', ['30', '13', '17']],
['Presov W', ['31', '13', '18']],
['Slavia Prague W', ['27', '10', '17']],
['Dicken', ['28', '14', '14']],
['Elbflorenz', ['24', '14', '10']],
['Lubeck-Schwartau', ['28', '12', '16']],
['HK Ogre/Miandum', ['28', '9', '19']],
['Stal Mielec', ['22', '13', '9']],
['MKS Perla Lublin W', ['30', '14', '16']],
['Koscierzyna W', ['22', '14', '8']],
['CS Madeira W', ['17', '8', '9']],
['CSM Focsani', ['26']],
['CSM Bucuresti', ['41', '21', '20']],
['Constanta', ['36', '18', '18']],
['Iasi', ['10']],
['Suceava', ['25', '12', '13']],
['Timisoara', ['27', '16', '11']],
['Saratov', ['31', '15', '16']],
['Alisa Ufa W', ['25', '15', '10']],
['Pozarevac', ['24', '8', '16']],
['Nove Zamky', ['28', '14', '14']],
['Aranas', ['24', '13', '11']],
['Ricoh', ['26', '14', '12']],
['H 65 Hoor W', ['33', '17', '16']],
['Lugi W', ['26', '12', '14']],
['Strands W', ['17', '12', '5']]], dtype=object)
Example 2
temp_list_column1b=['Benidorm',
'Alpla Hard',
'Dubrava',
'Frydek-Mistek',
'Karvina',
'Koprivnice',
'Nove Veseli',
'Vardar',
'Meble Elblag Wojcik',
'Zaglebie',
'Benfica',
'Barros W',
'Juvelis W',
'Assomada W',
'UOR No.2 Moscow',
'Izhevsk W',
'Stavropol W',
'Din. Volgograd W',
'Zvenigorod W',
'Adyif W',
'Crvena zvezda',
'Ribnica',
'Slovan',
'Jeruzalem Ormoz',
'Karlskrona',
'Torslanda W']
temp_list_column2b=[['28', '14', '14'],
['27', '12', '15'],
['24', '13', '11'],
['24', '14', '10'],
['28', '17', '11'],
['30', '16', '14'],
['26', '15', '11'],
['38', '18', '20'],
['24', '13', '11'],
['33', '15', '18'],
['24', '10', '14'],
['18', '11', '7'],
['22', '9', '13'],
['25', '12', '13'],
['19', '11', '8'],
['24', '10', '14'],
['21', '9', '12'],
['18', '10', '8'],
['31', '17', '14'],
['29', '15', '14'],
['26', '14', '12'],
['29', '12', '17'],
['25', '11', '14'],
['33', '19', '14'],
['32', '14', '18'],
['19', '12', '7']]
import numpy as np
temp_arrayb = np.column_stack((temp_list_column1b,temp_list_column2b))
output
array([['Benidorm', '28', '14', '14'],
['Alpla Hard', '27', '12', '15'],
['Dubrava', '24', '13', '11'],
['Frydek-Mistek', '24', '14', '10'],
['Karvina', '28', '17', '11'],
['Koprivnice', '30', '16', '14'],
['Nove Veseli', '26', '15', '11'],
['Vardar', '38', '18', '20'],
['Meble Elblag Wojcik', '24', '13', '11'],
['Zaglebie', '33', '15', '18'],
['Benfica', '24', '10', '14'],
['Barros W', '18', '11', '7'],
['Juvelis W', '22', '9', '13'],
['Assomada W', '25', '12', '13'],
['UOR No.2 Moscow', '19', '11', '8'],
['Izhevsk W', '24', '10', '14'],
['Stavropol W', '21', '9', '12'],
['Din. Volgograd W', '18', '10', '8'],
['Zvenigorod W', '31', '17', '14'],
['Adyif W', '29', '15', '14'],
['Crvena zvezda', '26', '14', '12'],
['Ribnica', '29', '12', '17'],
['Slovan', '25', '11', '14'],
['Jeruzalem Ormoz', '33', '19', '14'],
['Karlskrona', '32', '14', '18'],
['Torslanda W', '19', '12', '7']],
dtype='<U19')
In the first case, shape is (38, 2), while in the second is (26, 4) (i am interested in the number of columns only). Am I missing something obvious?
Your problem here seems to be that the first B list is jagged, while your second is rectangular.
Look at the difference in how Numpy converts the following two lists into Arrays (which, as #hpaulj points out, is exactly what happens when you pass them to column_stack:
In [1]: b1 = [
...: [1,2,3],
...: [2,3,4],
...: [3,4,5],
...: [4,5,6]]
In [2]: np.array(b1)
Out[2]:
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
In [3]: b2 = [
...: [1,2,3],
...: [2,3],
...: [3]]
In [4]: np.array(b2)
Out[4]: array([list([1, 2, 3]), list([2, 3]), list([3])], dtype=object)
Thus, when column stacking your example lists, in the first case you have a 1D array of lists that gets converted into a single column, whereas in the second case you have a 2D matrix of numbers that has 3 columns.
You should probably just not even be using Numpy's column_stack in this case, just zip the two lists together. If you want a numpy array as your final result, just np.array(list(zip(list_a, list_b)))
EDIT: In retrospect, your data structure sounds more like what's typically referred to as a DataFrame, rather than a matrix which is what Numpy is trying to give you.
import pandas as pd
data = pd.DataFrame()
data['name'] = temp_list_column1
data['numbers'] = test_list_column2
# Or
data = pd.DataFrame(list(zip(temp_list_column1, temp_list_column2)), columns=['name', 'numbers'])
Which gives you a data structure that looks like:
name numbers
0 John [1, 2, 3]
1 James [2, 3, 4]
2 Peter [3, 4, 5]
3 Paul [4, 5, 6]
Diagnosis
It seems like the issue is for the 2nd example, all the sublists has 3 elements while in the first example there are sublists with length 1 e.g. ['Bjerringbro W', ['26']]; the list ['26'] has only one element.
In the second case apparently np.column_stack forces to NOT HAVE lists as a cell element. In fact, we can have another discussion about why you want to see lists as cell elements which I will not go through here. Here is the solution
Special Case Solution
I assume you don't mind using pandas
import pandas as pd
series_1 = pd.Series(temp_list_column1b).to_frame(name='col1') # name it whatever you want
series_2 = pd.Series(temp_list_column2b).to_frame(name='col2') # name it whatever you want
df = pd.concat([series_1, series_2], axis=1)
# print(df) # view in pandas form
# print(df.values) # to see how it looks like as a numpy array
# print(df.values.shape) # to see how what the shape is in terms of numpy
Generalized Solution
Assuming you have a list of such columns which is called "list_of_cols". Then:
import pandas as pd
'''
list_of_cols: all the lists you want to combine
'''
df = pd.concat([pd.Series(temp_col).to_frame() for temp_col in list_of_cols], axis=1)
I hope this helps!
I am new to python. I need to create a list of integers from 1 to 70, but for each integer I want to make it a string and a comma after it and store it in another list.
Ex:
for i in range (1,71):
list_of_ints.append(i)
{ Some code
}
it should then be something like this
columns = ['1','2','3','4'.......'70']
Use [str(i) for i in range(1, 71)]. This gives you the list of str(i) for all i in range(1, 71). The function str(i) returns i as a str value instead of as an int
Seems like you want something like this,
>>> l = []
>>> for i in range(1,71):
l.append(str(i))
>>> l
['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']
new_list = [str(x) for x in range(1, 71)]
Using a list comprehension to achieve the same result.
You can use map to help you here:
>>> list_of_ints = range(1, 71)
>>> list_of_ints = map(str, list_of_ints)
>>> print list_of_ints
['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']
>>>