Sorting a multidimensional array using merge sort? - python
I am trying to sort this multidimensional array after the number on the first index using the merge sort algorithm, but I am very unsure on how to do so.
This is the multidimensional array I am trying to sort:
Orders_db = [[1347517405, 54413, '78'], [1347517413, 54421, '86'], [1347517454, 54462, '127'], [1347517460, 54468, '133'], [1347517461, 54469, '134'], [1347517426, 54434, '99'], [1347517464, 54472, '137'], [1347517394, 54402, '67'], [1347517445, 54453, '118'], [1347517375, 54383, '48'], [1347517377, 54385, '50'], [1347517392, 54400, '65'], [1347517450, 54458, '123'], [1347517404, 54412, '77'], [1347517389, 54397, '62'], [1347517393, 54401, '66'], [1347517440, 54448, '113'], [1347517457, 54465, '130'], [1347517444, 54452, '117'], [1347517400, 54408, '73'], [1347517412, 54420, '85'], [1347517371, 54379, '44'], [1347517415, 54423, '88'], [1347517441, 54449, '114'], [1347517435, 54443, '108'], [1347517409, 54417, '82'], [1347517398, 54406, '71'], [1347517422, 54430, '95'], [1347517468, 54476, '141'], [1347517402, 54410, '75'], [1347517437, 54445, '110'], [1347517446, 54454, '119'], [1347517382, 54390, '55'], [1347517399, 54407, '72'], [1347517438, 54446, '111'], [1347517416, 54424, '89'], [1347517380, 54388, '53'], [1347517425, 54433, '98'], [1347517406, 54414, '79'], [1347517449, 54457, '122'], [1347517388, 54396, '61'], [1347517430, 54438, '103'], [1347517455, 54463, '128'], [1347517458, 54466, '131'], [1347517452, 54460, '125'], [1347517396, 54404, '69'], [1347517423, 54431, '96'], [1347517465, 54473, '138'], [1347517397, 54405, '70'], [1347517459, 54467, '132'], [1347517395, 54403, '68'], [1347517381, 54389, '54'], [1347517424, 54432, '97'], [1347517436, 54444, '109'], [1347517434, 54442, '107'], [1347517401, 54409, '74'], [1347517376, 54384, '49'], [1347517467, 54475, '140'], [1347517456, 54464, '129'], [1347517427, 54435, '100'], [1347517383, 54391, '56'], [1347517451, 54459, '124'], [1347517433, 54441, '106'], [1347517414, 54422, '87'], [1347517417, 54425, '90'], [1347517453, 54461, '126'], [1347517378, 54386, '51'], [1347517432, 54440, '105'], [1347517403, 54411, '76'], [1347517439, 54447, '112'], [1347517448, 54456, '121'], [1347517410, 54418, '83'], [1347517391, 54399, '64'], [1347517447, 54455, '120'], [1347517421, 54429, '94'], [1347517379, 54387, '52'], [1347517411, 54419, '84'], [1347517386, 54394, '59'], [1347517384, 54392, '57'], [1347517374, 54382, '47'], [1347517462, 54470, '135'], [1347517431, 54439, '104'], [1347517419, 54427, '92'], [1347517428, 54436, '101'], [1347517466, 54474, '139'], [1347517443, 54451, '116'], [1347517463, 54471, '136'], [1347517385, 54393, '58'], [1347517387, 54395, '60'], [1347517373, 54381, '46'], [1347517372, 54380, '45'], [1347517418, 54426, '91'], [1347517420, 54428, '93'], [1347517469, 54477, '142]'], [1347517442, 54450, '115'], [1347517408, 54416, '81'], [1347517390, 54398, '63'], [1347517407, 54415, '80'], [1347517429, 54437, '102']]
And I can implement a general merge sort algorithm, but i cannot do it in a way where I sort after the number in the array on the first index.
My implementation of merge sort is:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1
return arr
How can I fit the general merge sort algorithm to sort this array?
The preferred answer is that it returns the array with the highest number in the end.
Try below:
def merge(left, right):
if not len(left) or not len(right):
return left or right
result = []
i, j = 0, 0
while (len(result) < len(left) + len(right)):
if left[i][0] < right[j][0]:
result.append(left[i])
i+= 1
else:
result.append(right[j])
j+= 1
if i == len(left) or j == len(right):
result.extend(left[i:] or right[j:])
break
return result
def mergesort(list):
if len(list) < 2:
return list
middle = len(list)/2
left = mergesort(list[:middle])
right = mergesort(list[middle:])
return merge(left, right)
seq = [[1347517405, 54413, '78'], [1347517413, 54421, '86'], [1347517454, 54462, '127'], [1347517460, 54468, '133'], [1347517461, 54469, '134'], [1347517426, 54434, '99'], [1347517464, 54472, '137'], [1347517394, 54402, '67'], [1347517445, 54453, '118'], [1347517375, 54383, '48'], [1347517377, 54385, '50'], [1347517392, 54400, '65'], [1347517450, 54458, '123'], [1347517404, 54412, '77'], [1347517389, 54397, '62'], [1347517393, 54401, '66'], [1347517440, 54448, '113'], [1347517457, 54465, '130'], [1347517444, 54452, '117'], [1347517400, 54408, '73'], [1347517412, 54420, '85'], [1347517371, 54379, '44'], [1347517415, 54423, '88'], [1347517441, 54449, '114'], [1347517435, 54443, '108'], [1347517409, 54417, '82'], [1347517398, 54406, '71'], [1347517422, 54430, '95'], [1347517468, 54476, '141'], [1347517402, 54410, '75'], [1347517437, 54445, '110'], [1347517446, 54454, '119'], [1347517382, 54390, '55'], [1347517399, 54407, '72'], [1347517438, 54446, '111'], [1347517416, 54424, '89'], [1347517380, 54388, '53'], [1347517425, 54433, '98'], [1347517406, 54414, '79'], [1347517449, 54457, '122'], [1347517388, 54396, '61'], [1347517430, 54438, '103'], [1347517455, 54463, '128'], [1347517458, 54466, '131'], [1347517452, 54460, '125'], [1347517396, 54404, '69'], [1347517423, 54431, '96'], [1347517465, 54473, '138'], [1347517397, 54405, '70'], [1347517459, 54467, '132'], [1347517395, 54403, '68'], [1347517381, 54389, '54'], [1347517424, 54432, '97'], [1347517436, 54444, '109'], [1347517434, 54442, '107'], [1347517401, 54409, '74'], [1347517376, 54384, '49'], [1347517467, 54475, '140'], [1347517456, 54464, '129'], [1347517427, 54435, '100'], [1347517383, 54391, '56'], [1347517451, 54459, '124'], [1347517433, 54441, '106'], [1347517414, 54422, '87'], [1347517417, 54425, '90'], [1347517453, 54461, '126'], [1347517378, 54386, '51'], [1347517432, 54440, '105'], [1347517403, 54411, '76'], [1347517439, 54447, '112'], [1347517448, 54456, '121'], [1347517410, 54418, '83'], [1347517391, 54399, '64'], [1347517447, 54455, '120'], [1347517421, 54429, '94'], [1347517379, 54387, '52'], [1347517411, 54419, '84'], [1347517386, 54394, '59'], [1347517384, 54392, '57'], [1347517374, 54382, '47'], [1347517462, 54470, '135'], [1347517431, 54439, '104'], [1347517419, 54427, '92'], [1347517428, 54436, '101'], [1347517466, 54474, '139'], [1347517443, 54451, '116'], [1347517463, 54471, '136'], [1347517385, 54393, '58'], [1347517387, 54395, '60'], [1347517373, 54381, '46'], [1347517372, 54380, '45'], [1347517418, 54426, '91'], [1347517420, 54428, '93'], [1347517469, 54477, '142]'], [1347517442, 54450, '115'], [1347517408, 54416, '81'], [1347517390, 54398, '63'], [1347517407, 54415, '80'], [1347517429, 54437, '102']]
print("Given array is")
print(seq);
print("\n")
print("Sorted array is")
print(mergesort(seq))
If you chage left[i][0] < right[j][0] to left[i][1] < right[j][1] then it will sort accrding to the second element in the inner array.
Related
Running 1000 functions gracefully using python multi-processing
I'm trying to receive stock data for about 1000 stocks, to speed up the process I'm using multiprocessing, unfortunately due to the large amount of stock data I'm trying to receive python as a whole just crashes. Is there a way to use multiprocessing without python crashing, I understand it would still take some time to do all of the 1000 stocks, but all I need is to do this process as fast as possible. import threading import yfinance as yf from multiprocessing import Process database = {} mylock = threading.RLock() stocks = ['AAU', 'ABEO', 'ABEV', 'ABIO', 'ABUS', 'ACCO', 'ACER', 'ACIU', 'ACOR', 'ACRX', 'ACST', 'ACTG', 'ADAP', 'ADIL', 'ADMA', 'ADMP', 'ADT', 'ADTX', 'ADXS', 'AEG', 'AEHL', 'AEHR', 'AEMD', 'AESE', 'AEY', 'AEZS', 'AFIN', 'AFMD', 'AGEN', 'AGI', 'AGRO', 'AGRX', 'AGS', 'AGTC', 'AHPI', 'AHT', 'AIHS', 'AIKI', 'AIM', 'AINC', 'AIRI', 'AIV', 'AKBA', 'AKER', 'AKTX', 'ALNA', 'ALRN', 'ALSK', 'AM', 'AMBO', 'AMC', 'AMPE', 'AMPY', 'AMRN', 'AMRS', 'AMRX', 'AMTX', 'ANCN', 'ANH', 'ANIX', 'ANPC', 'ANTE', 'ANY', 'APDN', 'APM', 'APRE', 'APRN', 'APTO', 'APTS', 'APTX', 'APWC', 'AQMS', 'AQST', 'AR', 'ARAY', 'ARC', 'ARCO', 'ARDX', 'AREC', 'ARKO', 'ARLO', 'ARLP', 'AROC', 'ARPO', 'ARTL', 'ASC', 'ASLN', 'ASM', 'ASMB', 'ASRT', 'ASTC', 'ASX', 'ATAX', 'ATHE', 'ATHX', 'ATIF', 'ATNF', 'ATNM', 'ATOS', 'ATRS', 'ATXI', 'AUMN', 'AUTO', 'AUVI', 'AUY', 'AVCO', 'AVDL', 'AVEO', 'AVGR', 'AVXL', 'AWH', 'AWX', 'AXAS', 'AXL', 'AXU', 'AYRO', 'AYTU', 'AZRX', 'BBAR', 'BBD', 'BBGI', 'BBI', 'BBIG', 'BBVA', 'BBW', 'BCDA', 'BCLI', 'BCRX', 'BCS', 'BDR', 'BDSI', 'BEST', 'BGCP', 'BGI', 'BHAT', 'BHR', 'BHTG', 'BIMI', 'BIOC', 'BIOL', 'BKCC', 'BKD', 'BKEP', 'BKYI', 'BLCM', 'BLCT', 'BLIN', 'BLRX', 'BLU', 'BMRA', 'BNED', 'BNTC', 'BORR', 'BOXL', 'BPT', 'BPTH', 'BQ', 'BREZR', 'BRFS', 'BRN', 'BRPAR', 'BRQS', 'BRY', 'BSBR', 'BSGM', 'BSM', 'BSMX', 'BTG', 'BTU', 'BVXV', 'BW', 'BWEN', 'BXRX', 'BYFC', 'CAAP', 'CAAS', 'CALA', 'CAN', 'CANF', 'CAPR', 'CARV', 'CASA', 'CASI', 'CATB', 'CBAT', 'CBAY', 'CBIO', 'CBLI', 'CCO', 'CCRC', 'CCRN', 'CDE', 'CDEV', 'CDTX', 'CDXC', 'CEI', 'CEIX', 'CEMI', 'CERC', 'CERS', 'CETX', 'CFMS', 'CGIX', 'CHEK', 'CHMA', 'CHNR', 'CHRA', 'CHS', 'CHU', 'CIDM', 'CIG', 'CIO', 'CJJD', 'CKPT', 'CLBS', 'CLIR', 'CLNC', 'CLNY', 'CLPS', 'CLRB', 'CLS', 'CLSD', 'CLSN', 'CLVR', 'CLVS', 'CLXT', 'CMCM', 'CMO', 'CMRE', 'CMRX', 'CNDT', 'CNET', 'CNFR', 'CNSL', 'CNSP', 'CNTY', 'COCP', 'COGT', 'COMS', 'CORR', 'CPG', 'CPHI', 'CPRX', 'CPSH', 'CRBP', 'CREG', 'CREX', 'CRIS', 'CRK', 'CRKN', 'CRMD', 'CRNT', 'CSCW', 'CSLT', 'CSPR', 'CTEK', 'CTIB', 'CTIC', 'CTK', 'CTMX', 'CTRM', 'CTSO', 'CTXR', 'CVE', 'CVGI', 'CWBR', 'CX', 'CXW', 'CYCC', 'CYCN', 'CYRN', 'CYTH', 'DARE', 'DBVT', 'DFFN', 'DGLY', 'DHC', 'DHT', 'DLPN', 'DNK', 'DNN', 'DNOW', 'DOGZ', 'DPW', 'DRH', 'DRRX', 'DRTT', 'DS', 'DSKE', 'DSS', 'DSSI', 'DSX', 'DTEA', 'DTSS', 'DUO', 'DVAX', 'DXF', 'DXLG', 'DYNT', 'EARS', 'EBON', 'EBR', 'ECOR', 'EDSA', 'EGY', 'EIGR', 'ELVT', 'ELYS', 'EMAN', 'EMKR', 'EMX', 'ENBL', 'ENDP', 'ENG', 'ENIA', 'ENIC', 'ENLC', 'ENSV', 'ENTX', 'ENVB', 'ENZ', 'EOLS', 'EQ', 'EQX', 'ERF', 'ERJ', 'ESGC', 'ESTE', 'ET', 'ETM', 'ETRN', 'ETTX', 'EURN', 'EVC', 'EVFM', 'EVGN', 'EVK', 'EVOK', 'EXK', 'EXPR', 'EXTR', 'EYEG', 'EYES', 'EZGO', 'EZPW', 'FAMI', 'FBIO', 'FBP', 'FENG', 'FI', 'FINV', 'FLDM', 'FLMN', 'FLNT', 'FLY', 'FORD', 'FPAY', 'FRBK', 'FRO', 'FRSX', 'FSM', 'FSP', 'FTEK', 'FTFT', 'FTK', 'FURY', 'GAU', 'GBS', 'GCI', 'GEL', 'GEN', 'GENE', 'GEO', 'GERN', 'GFI', 'GGAL', 'GGB', 'GHSI', 'GLBS', 'GLDG', 'GLG', 'GLOG', 'GLOP', 'GLUU', 'GLYC', 'GMBL', 'GMDA', 'GMLP', 'GNCA', 'GNK', 'GNLN', 'GNPX', 'GNUS', 'GNW', 'GOGL', 'GOL', 'GORO', 'GOSS', 'GOVX', 'GPL', 'GPMT', 'GPRO', 'GRIL', 'GRNQ', 'GSAT', 'GSKY', 'GSM', 'GSS', 'GSV', 'GTE', 'GTEC', 'GTT', 'GV', 'GVP', 'HAPP', 'HBM', 'HCDI', 'HCHC', 'HDSN', 'HEPA', 'HEXO', 'HGSH', 'HIL', 'HIMX', 'HJLI', 'HL', 'HLIT', 'HLX', 'HMHC', 'HMY', 'HNRG', 'HOFV', 'HOTH', 'HSTO', 'HT', 'HTBX', 'HUGE', 'HUSA', 'HUSN', 'HX', 'HYRE', 'IAG', 'IBIO', 'ICD', 'ICON', 'ID', 'IDEX', 'IDRA', 'IFMK', 'IFRX', 'IGC', 'IHT', 'IKT', 'IMAC', 'IMGN', 'IMMP', 'IMTE', 'IMV', 'INDO', 'INFI', 'ING', 'INN', 'INOD', 'INPX', 'INUV', 'IO', 'IPDN', 'IRIX', 'ISEE', 'ISIG', 'ISR', 'ITP', 'ITRM', 'ITUB', 'IVR', 'IZEA', 'JAGX', 'JE', 'JFIN', 'JFU', 'JG', 'JIH.W', 'JILL', 'JOB', 'JUPW', 'KALA', 'KBNT', 'KBSF', 'KDMN', 'KERN', 'KGC', 'KIN', 'KIQ', 'KMPH', 'KNDI', 'KODK', 'KOPN', 'KOS', 'KRKR', 'KRMD', 'KTRA', 'KUKE', 'KXIN', 'KZIA', 'LCI', 'LCTX', 'LEAF', 'LEE', 'LGHL', 'LIFE', 'LITB', 'LIVX', 'LIZI', 'LJPC', 'LKCO', 'LLIT', 'LLNW', 'LMFA', 'LMNL', 'LODE', 'LOMA', 'LPCN', 'LPTH', 'LPTX', 'LQDA', 'LSEA', 'LTBR', 'LTRPA', 'LX', 'LXRX', 'LYG', 'MACK', 'MARK', 'MBI', 'MBII', 'MBIO', 'MBRX', 'MBT', 'MCEP', 'MCF', 'MDGS', 'MDXG', 'MEIP', 'MESA', 'MESO', 'METX', 'MFA', 'MFG', 'MFGP', 'MFH', 'MGI', 'MGY', 'MHLD', 'MICT', 'MIN', 'MIND', 'MITO', 'MITT', 'MKD', 'MKGI', 'MLND', 'MLSS', 'MNKD', 'MOGO', 'MOGU', 'MOHO', 'MOSY', 'MOTS', 'MOXC', 'MPLN', 'MRC', 'MREO', 'MRIN', 'MRKR', 'MRO', 'MSN', 'MTA', 'MTC', 'MTL', 'MTNB', 'MTP', 'MTSL', 'MUFG', 'MUX', 'MVIS', 'MYSZ', 'MYT', 'NAK', 'NAKD', 'NAOV', 'NAT', 'NAVB', 'NBEV', 'NBRV', 'NBSE', 'NBY', 'NCMI', 'NCNA', 'NDRA', 'NEOS', 'NEPT', 'NERV', 'NES', 'NEW', 'NEX', 'NG', 'NGD', 'NGL', 'NH', 'NLY', 'NMCI', 'NMRK', 'NMTR', 'NNVC', 'NOK', 'NOVN', 'NR', 'NRZ', 'NSCO', 'NSPR', 'NTEC', 'NTN', 'NURO', 'NVCN', 'NVIV', 'NWG', 'NXE', 'NXTD', 'NYMT', 'OBLG', 'OBLN', 'OBSV', 'OCG', 'OCGN', 'OCSL', 'OCX', 'OEG', 'OGEN', 'OGI', 'OIBR.C', 'OII', 'OIIM', 'OIS', 'ONCT', 'ONCY', 'ONTX', 'OPGN', 'OPK', 'OPTN', 'OPTT', 'ORBC', 'ORC', 'ORMP', 'ORN', 'ORTX', 'OSMT', 'OSW', 'OTIC', 'OTLK', 'OVID', 'OXBR', 'OXLC', 'PAA', 'PAE', 'PAGP', 'PAVM', 'PAYS', 'PBF', 'PBI', 'PDSB', 'PED', 'PEI', 'PEIX', 'PFMT', 'PGEN', 'PGRE', 'PHAS', 'PHIO', 'PHUN', 'PIRS', 'PIXY', 'PLAG', 'PLG', 'PLIN', 'PLM', 'PLYA', 'PNNT', 'POAI', 'POWW', 'PPBT', 'PPR', 'PPSI', 'PPT', 'PROG', 'PRPO', 'PRQR', 'PRTK', 'PRTY', 'PSEC', 'PSTI', 'PSTV', 'PT', 'PTE', 'PTEN', 'PTMN', 'PTN', 'PULM', 'PUMP', 'PVL', 'PXLW', 'PXS', 'QD', 'QEP', 'QIWI', 'QLGN', 'QLI', 'QTNT', 'QTT', 'QUAD', 'QUOT', 'RAIL', 'RAVE', 'RBBN', 'RCON', 'RDHL', 'REED', 'REFR', 'REI', 'REPH', 'RES', 'RESN', 'RETO', 'RFP', 'RGLS', 'RGS', 'RHE', 'RIBT', 'RIG', 'RIGL', 'RKDA', 'RLH', 'RMED', 'RMTI', 'RNWK', 'RPAI', 'RPT', 'RRC', 'RRD', 'RTLR', 'RUHN', 'RWLK', 'RWT', 'RYAM', 'SALM', 'SAN', 'SAND', 'SB', 'SBBP', 'SBS', 'SCKT', 'SCOR', 'SCYX', 'SD', 'SDPI', 'SEAC', 'SEEL', 'SELB', 'SENS', 'SESN', 'SFET', 'SFL', 'SFT', 'SGBX', 'SGLB', 'SGOC', 'SHIP', 'SID', 'SIEN', 'SIF', 'SIFY', 'SILV', 'SINO', 'SINT', 'SIOX', 'SIRI', 'SLCA', 'SLDB', 'SLGG', 'SLRX', 'SLS', 'SM', 'SMFG', 'SMSI', 'SMTS', 'SMTX', 'SNCA', 'SNCR', 'SND', 'SNDE', 'SNDL', 'SNES', 'SNGX', 'SNMP', 'SNOA', 'SNR', 'SNSS', 'SOI', 'SOLO', 'SONM', 'SONN', 'SOS', 'SPCB', 'SPPI', 'SQFT', 'SQNS', 'SREV', 'SRGA', 'SSL', 'STAF', 'STCN', 'STON', 'STSA', 'SUP', 'SUPV', 'SVM', 'SVRA', 'SWN', 'SXC', 'SXTC', 'SYBX', 'SYN', 'SYPR', 'TACO', 'TALO', 'TANH', 'TAOP', 'TAST', 'TAT', 'TATT', 'TBLT', 'TCCO', 'TCDA', 'TCON', 'TEDU', 'TEF', 'TELL', 'TENX', 'TEO', 'TGA', 'TGB', 'TGC', 'THM', 'THMO', 'TK', 'TKAT', 'TKC', 'TLGT', 'TLMD', 'TLSA', 'TLYS', 'TMBR', 'TMDI', 'TMQ', 'TMST', 'TNAV', 'TNXP', 'TOPS', 'TOUR', 'TPRE', 'TRCH', 'TRIB', 'TRIT', 'TRST', 'TRUE', 'TRVG', 'TRVN', 'TRX', 'TRXC', 'TTI', 'TTNP', 'TTOO', 'TUSK', 'TV', 'TWI', 'TWO', 'TXMD', 'TYME', 'UAMY', 'UBX', 'UEC', 'UEPS', 'UGP', 'UMC', 'UONE', 'UONEK', 'URG', 'USAS', 'USAT', 'USEG', 'USIO', 'USWS', 'USX', 'UTSI', 'UUUU', 'UWMC', 'UXIN', 'VBIV', 'VBLT', 'VCNX', 'VEDL', 'VEON', 'VERB', 'VERO', 'VERU', 'VET', 'VGZ', 'VHC', 'VIOT', 'VIRI', 'VISL', 'VIV', 'VIVE', 'VKTX', 'VNTR', 'VRA', 'VRAY', 'VSTM', 'VTGN', 'VTNR', 'VTVT', 'VVOS', 'VXRT', 'VYGR', 'VYNE', 'WATT', 'WEI', 'WETF', 'WIMI', 'WISA', 'WIT', 'WKEY', 'WMC', 'WORX', 'WPRT', 'WPX', 'WRAP', 'WRN', 'WSR', 'WTER', 'WTI', 'WTRH', 'WTTR', 'WVE', 'WWR', 'XAIR', 'XAN', 'XBIO', 'XCUR', 'XELA', 'XELB', 'XERS', 'XNET', 'XPL', 'XSPA', 'XXII', 'YCBD', 'YGYI', 'YJ', 'YPF', 'YRCW', 'YTRA', 'YVR', 'ZAGG', 'ZIOP', 'ZIXI', 'ZKIN', 'ZNGA', 'ZOM', 'ZSAN', 'ZVO', 'ZYNE', ''] class info(): def __init__(self, name): self.name = name self.goo() def goo(self): self.x = {'ticker':self.name, 'name': str(yf.Ticker(self.name).info['longName'])} print(self.x, ) with mylock: database[self.name] = self.x def run_in_p(): proc = [] for name_s in stocks: p = Process(target=info, args=(name_s, )) p.start() proc.append(p) for p in proc: p.join() print(database) if __name__ == "__main__": run_in_p() Edit: Here's the error window that pops up Along with this error window I get another error in the python console raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: Not Found
I'd like to offer a solution using a package called yahooquery. Disclaimer: I am the author of the package. You can get that same data in a few seconds with the following: from yahooquery import Ticker stocks = ['AAU', 'ABEO', 'ABEV', 'ABIO', 'ABUS', 'ACCO', 'ACER', 'ACIU', 'ACOR', 'ACRX', 'ACST', 'ACTG', 'ADAP', 'ADIL', 'ADMA', 'ADMP', 'ADT', 'ADTX', 'ADXS', 'AEG', 'AEHL', 'AEHR', 'AEMD', 'AESE', 'AEY', 'AEZS', 'AFIN', 'AFMD', 'AGEN', 'AGI', 'AGRO', 'AGRX', 'AGS', 'AGTC', 'AHPI', 'AHT', 'AIHS', 'AIKI', 'AIM', 'AINC', 'AIRI', 'AIV', 'AKBA', 'AKER', 'AKTX', 'ALNA', 'ALRN', 'ALSK', 'AM', 'AMBO', 'AMC', 'AMPE', 'AMPY', 'AMRN', 'AMRS', 'AMRX', 'AMTX', 'ANCN', 'ANH', 'ANIX', 'ANPC', 'ANTE', 'ANY', 'APDN', 'APM', 'APRE', 'APRN', 'APTO', 'APTS', 'APTX', 'APWC', 'AQMS', 'AQST', 'AR', 'ARAY', 'ARC', 'ARCO', 'ARDX', 'AREC', 'ARKO', 'ARLO', 'ARLP', 'AROC', 'ARPO', 'ARTL', 'ASC', 'ASLN', 'ASM', 'ASMB', 'ASRT', 'ASTC', 'ASX', 'ATAX', 'ATHE', 'ATHX', 'ATIF', 'ATNF', 'ATNM', 'ATOS', 'ATRS', 'ATXI', 'AUMN', 'AUTO', 'AUVI', 'AUY', 'AVCO', 'AVDL', 'AVEO', 'AVGR', 'AVXL', 'AWH', 'AWX', 'AXAS', 'AXL', 'AXU', 'AYRO', 'AYTU', 'AZRX', 'BBAR', 'BBD', 'BBGI', 'BBI', 'BBIG', 'BBVA', 'BBW', 'BCDA', 'BCLI', 'BCRX', 'BCS', 'BDR', 'BDSI', 'BEST', 'BGCP', 'BGI', 'BHAT', 'BHR', 'BHTG', 'BIMI', 'BIOC', 'BIOL', 'BKCC', 'BKD', 'BKEP', 'BKYI', 'BLCM', 'BLCT', 'BLIN', 'BLRX', 'BLU', 'BMRA', 'BNED', 'BNTC', 'BORR', 'BOXL', 'BPT', 'BPTH', 'BQ', 'BREZR', 'BRFS', 'BRN', 'BRPAR', 'BRQS', 'BRY', 'BSBR', 'BSGM', 'BSM', 'BSMX', 'BTG', 'BTU', 'BVXV', 'BW', 'BWEN', 'BXRX', 'BYFC', 'CAAP', 'CAAS', 'CALA', 'CAN', 'CANF', 'CAPR', 'CARV', 'CASA', 'CASI', 'CATB', 'CBAT', 'CBAY', 'CBIO', 'CBLI', 'CCO', 'CCRC', 'CCRN', 'CDE', 'CDEV', 'CDTX', 'CDXC', 'CEI', 'CEIX', 'CEMI', 'CERC', 'CERS', 'CETX', 'CFMS', 'CGIX', 'CHEK', 'CHMA', 'CHNR', 'CHRA', 'CHS', 'CHU', 'CIDM', 'CIG', 'CIO', 'CJJD', 'CKPT', 'CLBS', 'CLIR', 'CLNC', 'CLNY', 'CLPS', 'CLRB', 'CLS', 'CLSD', 'CLSN', 'CLVR', 'CLVS', 'CLXT', 'CMCM', 'CMO', 'CMRE', 'CMRX', 'CNDT', 'CNET', 'CNFR', 'CNSL', 'CNSP', 'CNTY', 'COCP', 'COGT', 'COMS', 'CORR', 'CPG', 'CPHI', 'CPRX', 'CPSH', 'CRBP', 'CREG', 'CREX', 'CRIS', 'CRK', 'CRKN', 'CRMD', 'CRNT', 'CSCW', 'CSLT', 'CSPR', 'CTEK', 'CTIB', 'CTIC', 'CTK', 'CTMX', 'CTRM', 'CTSO', 'CTXR', 'CVE', 'CVGI', 'CWBR', 'CX', 'CXW', 'CYCC', 'CYCN', 'CYRN', 'CYTH', 'DARE', 'DBVT', 'DFFN', 'DGLY', 'DHC', 'DHT', 'DLPN', 'DNK', 'DNN', 'DNOW', 'DOGZ', 'DPW', 'DRH', 'DRRX', 'DRTT', 'DS', 'DSKE', 'DSS', 'DSSI', 'DSX', 'DTEA', 'DTSS', 'DUO', 'DVAX', 'DXF', 'DXLG', 'DYNT', 'EARS', 'EBON', 'EBR', 'ECOR', 'EDSA', 'EGY', 'EIGR', 'ELVT', 'ELYS', 'EMAN', 'EMKR', 'EMX', 'ENBL', 'ENDP', 'ENG', 'ENIA', 'ENIC', 'ENLC', 'ENSV', 'ENTX', 'ENVB', 'ENZ', 'EOLS', 'EQ', 'EQX', 'ERF', 'ERJ', 'ESGC', 'ESTE', 'ET', 'ETM', 'ETRN', 'ETTX', 'EURN', 'EVC', 'EVFM', 'EVGN', 'EVK', 'EVOK', 'EXK', 'EXPR', 'EXTR', 'EYEG', 'EYES', 'EZGO', 'EZPW', 'FAMI', 'FBIO', 'FBP', 'FENG', 'FI', 'FINV', 'FLDM', 'FLMN', 'FLNT', 'FLY', 'FORD', 'FPAY', 'FRBK', 'FRO', 'FRSX', 'FSM', 'FSP', 'FTEK', 'FTFT', 'FTK', 'FURY', 'GAU', 'GBS', 'GCI', 'GEL', 'GEN', 'GENE', 'GEO', 'GERN', 'GFI', 'GGAL', 'GGB', 'GHSI', 'GLBS', 'GLDG', 'GLG', 'GLOG', 'GLOP', 'GLUU', 'GLYC', 'GMBL', 'GMDA', 'GMLP', 'GNCA', 'GNK', 'GNLN', 'GNPX', 'GNUS', 'GNW', 'GOGL', 'GOL', 'GORO', 'GOSS', 'GOVX', 'GPL', 'GPMT', 'GPRO', 'GRIL', 'GRNQ', 'GSAT', 'GSKY', 'GSM', 'GSS', 'GSV', 'GTE', 'GTEC', 'GTT', 'GV', 'GVP', 'HAPP', 'HBM', 'HCDI', 'HCHC', 'HDSN', 'HEPA', 'HEXO', 'HGSH', 'HIL', 'HIMX', 'HJLI', 'HL', 'HLIT', 'HLX', 'HMHC', 'HMY', 'HNRG', 'HOFV', 'HOTH', 'HSTO', 'HT', 'HTBX', 'HUGE', 'HUSA', 'HUSN', 'HX', 'HYRE', 'IAG', 'IBIO', 'ICD', 'ICON', 'ID', 'IDEX', 'IDRA', 'IFMK', 'IFRX', 'IGC', 'IHT', 'IKT', 'IMAC', 'IMGN', 'IMMP', 'IMTE', 'IMV', 'INDO', 'INFI', 'ING', 'INN', 'INOD', 'INPX', 'INUV', 'IO', 'IPDN', 'IRIX', 'ISEE', 'ISIG', 'ISR', 'ITP', 'ITRM', 'ITUB', 'IVR', 'IZEA', 'JAGX', 'JE', 'JFIN', 'JFU', 'JG', 'JIH.W', 'JILL', 'JOB', 'JUPW', 'KALA', 'KBNT', 'KBSF', 'KDMN', 'KERN', 'KGC', 'KIN', 'KIQ', 'KMPH', 'KNDI', 'KODK', 'KOPN', 'KOS', 'KRKR', 'KRMD', 'KTRA', 'KUKE', 'KXIN', 'KZIA', 'LCI', 'LCTX', 'LEAF', 'LEE', 'LGHL', 'LIFE', 'LITB', 'LIVX', 'LIZI', 'LJPC', 'LKCO', 'LLIT', 'LLNW', 'LMFA', 'LMNL', 'LODE', 'LOMA', 'LPCN', 'LPTH', 'LPTX', 'LQDA', 'LSEA', 'LTBR', 'LTRPA', 'LX', 'LXRX', 'LYG', 'MACK', 'MARK', 'MBI', 'MBII', 'MBIO', 'MBRX', 'MBT', 'MCEP', 'MCF', 'MDGS', 'MDXG', 'MEIP', 'MESA', 'MESO', 'METX', 'MFA', 'MFG', 'MFGP', 'MFH', 'MGI', 'MGY', 'MHLD', 'MICT', 'MIN', 'MIND', 'MITO', 'MITT', 'MKD', 'MKGI', 'MLND', 'MLSS', 'MNKD', 'MOGO', 'MOGU', 'MOHO', 'MOSY', 'MOTS', 'MOXC', 'MPLN', 'MRC', 'MREO', 'MRIN', 'MRKR', 'MRO', 'MSN', 'MTA', 'MTC', 'MTL', 'MTNB', 'MTP', 'MTSL', 'MUFG', 'MUX', 'MVIS', 'MYSZ', 'MYT', 'NAK', 'NAKD', 'NAOV', 'NAT', 'NAVB', 'NBEV', 'NBRV', 'NBSE', 'NBY', 'NCMI', 'NCNA', 'NDRA', 'NEOS', 'NEPT', 'NERV', 'NES', 'NEW', 'NEX', 'NG', 'NGD', 'NGL', 'NH', 'NLY', 'NMCI', 'NMRK', 'NMTR', 'NNVC', 'NOK', 'NOVN', 'NR', 'NRZ', 'NSCO', 'NSPR', 'NTEC', 'NTN', 'NURO', 'NVCN', 'NVIV', 'NWG', 'NXE', 'NXTD', 'NYMT', 'OBLG', 'OBLN', 'OBSV', 'OCG', 'OCGN', 'OCSL', 'OCX', 'OEG', 'OGEN', 'OGI', 'OIBR.C', 'OII', 'OIIM', 'OIS', 'ONCT', 'ONCY', 'ONTX', 'OPGN', 'OPK', 'OPTN', 'OPTT', 'ORBC', 'ORC', 'ORMP', 'ORN', 'ORTX', 'OSMT', 'OSW', 'OTIC', 'OTLK', 'OVID', 'OXBR', 'OXLC', 'PAA', 'PAE', 'PAGP', 'PAVM', 'PAYS', 'PBF', 'PBI', 'PDSB', 'PED', 'PEI', 'PEIX', 'PFMT', 'PGEN', 'PGRE', 'PHAS', 'PHIO', 'PHUN', 'PIRS', 'PIXY', 'PLAG', 'PLG', 'PLIN', 'PLM', 'PLYA', 'PNNT', 'POAI', 'POWW', 'PPBT', 'PPR', 'PPSI', 'PPT', 'PROG', 'PRPO', 'PRQR', 'PRTK', 'PRTY', 'PSEC', 'PSTI', 'PSTV', 'PT', 'PTE', 'PTEN', 'PTMN', 'PTN', 'PULM', 'PUMP', 'PVL', 'PXLW', 'PXS', 'QD', 'QEP', 'QIWI', 'QLGN', 'QLI', 'QTNT', 'QTT', 'QUAD', 'QUOT', 'RAIL', 'RAVE', 'RBBN', 'RCON', 'RDHL', 'REED', 'REFR', 'REI', 'REPH', 'RES', 'RESN', 'RETO', 'RFP', 'RGLS', 'RGS', 'RHE', 'RIBT', 'RIG', 'RIGL', 'RKDA', 'RLH', 'RMED', 'RMTI', 'RNWK', 'RPAI', 'RPT', 'RRC', 'RRD', 'RTLR', 'RUHN', 'RWLK', 'RWT', 'RYAM', 'SALM', 'SAN', 'SAND', 'SB', 'SBBP', 'SBS', 'SCKT', 'SCOR', 'SCYX', 'SD', 'SDPI', 'SEAC', 'SEEL', 'SELB', 'SENS', 'SESN', 'SFET', 'SFL', 'SFT', 'SGBX', 'SGLB', 'SGOC', 'SHIP', 'SID', 'SIEN', 'SIF', 'SIFY', 'SILV', 'SINO', 'SINT', 'SIOX', 'SIRI', 'SLCA', 'SLDB', 'SLGG', 'SLRX', 'SLS', 'SM', 'SMFG', 'SMSI', 'SMTS', 'SMTX', 'SNCA', 'SNCR', 'SND', 'SNDE', 'SNDL', 'SNES', 'SNGX', 'SNMP', 'SNOA', 'SNR', 'SNSS', 'SOI', 'SOLO', 'SONM', 'SONN', 'SOS', 'SPCB', 'SPPI', 'SQFT', 'SQNS', 'SREV', 'SRGA', 'SSL', 'STAF', 'STCN', 'STON', 'STSA', 'SUP', 'SUPV', 'SVM', 'SVRA', 'SWN', 'SXC', 'SXTC', 'SYBX', 'SYN', 'SYPR', 'TACO', 'TALO', 'TANH', 'TAOP', 'TAST', 'TAT', 'TATT', 'TBLT', 'TCCO', 'TCDA', 'TCON', 'TEDU', 'TEF', 'TELL', 'TENX', 'TEO', 'TGA', 'TGB', 'TGC', 'THM', 'THMO', 'TK', 'TKAT', 'TKC', 'TLGT', 'TLMD', 'TLSA', 'TLYS', 'TMBR', 'TMDI', 'TMQ', 'TMST', 'TNAV', 'TNXP', 'TOPS', 'TOUR', 'TPRE', 'TRCH', 'TRIB', 'TRIT', 'TRST', 'TRUE', 'TRVG', 'TRVN', 'TRX', 'TRXC', 'TTI', 'TTNP', 'TTOO', 'TUSK', 'TV', 'TWI', 'TWO', 'TXMD', 'TYME', 'UAMY', 'UBX', 'UEC', 'UEPS', 'UGP', 'UMC', 'UONE', 'UONEK', 'URG', 'USAS', 'USAT', 'USEG', 'USIO', 'USWS', 'USX', 'UTSI', 'UUUU', 'UWMC', 'UXIN', 'VBIV', 'VBLT', 'VCNX', 'VEDL', 'VEON', 'VERB', 'VERO', 'VERU', 'VET', 'VGZ', 'VHC', 'VIOT', 'VIRI', 'VISL', 'VIV', 'VIVE', 'VKTX', 'VNTR', 'VRA', 'VRAY', 'VSTM', 'VTGN', 'VTNR', 'VTVT', 'VVOS', 'VXRT', 'VYGR', 'VYNE', 'WATT', 'WEI', 'WETF', 'WIMI', 'WISA', 'WIT', 'WKEY', 'WMC', 'WORX', 'WPRT', 'WPX', 'WRAP', 'WRN', 'WSR', 'WTER', 'WTI', 'WTRH', 'WTTR', 'WVE', 'WWR', 'XAIR', 'XAN', 'XBIO', 'XCUR', 'XELA', 'XELB', 'XERS', 'XNET', 'XPL', 'XSPA', 'XXII', 'YCBD', 'YGYI', 'YJ', 'YPF', 'YRCW', 'YTRA', 'YVR', 'ZAGG', 'ZIOP', 'ZIXI', 'ZKIN', 'ZNGA', 'ZOM', 'ZSAN', 'ZVO', 'ZYNE', ''] # validate is optional but will go through your list and keep only valid symbols t = Ticker(symbols, validate=True) data = t.quotes d = {k: v['longName'] for k, v in data.items()}
Ok, here is one way to obtain what you want in about 2min. Some tickers are bad, that's why it crashes. Here's the code. I use joblib for threading or multiprocess since it doesn't work in my env. But, that's the spirit. %%time import joblib from joblib import Parallel,delayed WRONG_TICKERS = [] database = {} stocks = ['AAU', 'ABEO', 'ABEV', 'ABIO', 'ABUS', 'ACCO', 'ACER', 'ACIU', 'ACOR', 'ACRX', 'ACST', 'ACTG', 'ADAP', 'ADIL', 'ADMA', 'ADMP', 'ADT', 'ADTX', 'ADXS', 'AEG', 'AEHL', 'AEHR', 'AEMD', 'AESE', 'AEY', 'AEZS', 'AFIN', 'AFMD', 'AGEN', 'AGI', 'AGRO', 'AGRX', 'AGS', 'AGTC', 'AHPI', 'AHT', 'AIHS', 'AIKI', 'AIM', 'AINC', 'AIRI', 'AIV', 'AKBA', 'AKER', 'AKTX', 'ALNA', 'ALRN', 'ALSK', 'AM', 'AMBO', 'AMC', 'AMPE', 'AMPY', 'AMRN', 'AMRS', 'AMRX', 'AMTX', 'ANCN', 'ANH', 'ANIX', 'ANPC', 'ANTE', 'ANY', 'APDN', 'APM', 'APRE', 'APRN', 'APTO', 'APTS', 'APTX', 'APWC', 'AQMS', 'AQST', 'AR', 'ARAY', 'ARC', 'ARCO', 'ARDX', 'AREC', 'ARKO', 'ARLO', 'ARLP', 'AROC', 'ARPO', 'ARTL', 'ASC', 'ASLN', 'ASM', 'ASMB', 'ASRT', 'ASTC', 'ASX', 'ATAX', 'ATHE', 'ATHX', 'ATIF', 'ATNF', 'ATNM', 'ATOS', 'ATRS', 'ATXI', 'AUMN', 'AUTO', 'AUVI', 'AUY', 'AVCO', 'AVDL', 'AVEO', 'AVGR', 'AVXL', 'AWH', 'AWX', 'AXAS', 'AXL', 'AXU', 'AYRO', 'AYTU', 'AZRX', 'BBAR', 'BBD', 'BBGI', 'BBI', 'BBIG', 'BBVA', 'BBW', 'BCDA', 'BCLI', 'BCRX', 'BCS', 'BDR', 'BDSI', 'BEST', 'BGCP', 'BGI', 'BHAT', 'BHR', 'BHTG', 'BIMI', 'BIOC', 'BIOL', 'BKCC', 'BKD', 'BKEP', 'BKYI', 'BLCM', 'BLCT', 'BLIN', 'BLRX', 'BLU', 'BMRA', 'BNED', 'BNTC', 'BORR', 'BOXL', 'BPT', 'BPTH', 'BQ', 'BREZR', 'BRFS', 'BRN', 'BRPAR', 'BRQS', 'BRY', 'BSBR', 'BSGM', 'BSM', 'BSMX', 'BTG', 'BTU', 'BVXV', 'BW', 'BWEN', 'BXRX', 'BYFC', 'CAAP', 'CAAS', 'CALA', 'CAN', 'CANF', 'CAPR', 'CARV', 'CASA', 'CASI', 'CATB', 'CBAT', 'CBAY', 'CBIO', 'CBLI', 'CCO', 'CCRC', 'CCRN', 'CDE', 'CDEV', 'CDTX', 'CDXC', 'CEI', 'CEIX', 'CEMI', 'CERC', 'CERS', 'CETX', 'CFMS', 'CGIX', 'CHEK', 'CHMA', 'CHNR', 'CHRA', 'CHS', 'CHU', 'CIDM', 'CIG', 'CIO', 'CJJD', 'CKPT', 'CLBS', 'CLIR', 'CLNC', 'CLNY', 'CLPS', 'CLRB', 'CLS', 'CLSD', 'CLSN', 'CLVR', 'CLVS', 'CLXT', 'CMCM', 'CMO', 'CMRE', 'CMRX', 'CNDT', 'CNET', 'CNFR', 'CNSL', 'CNSP', 'CNTY', 'COCP', 'COGT', 'COMS', 'CORR', 'CPG', 'CPHI', 'CPRX', 'CPSH', 'CRBP', 'CREG', 'CREX', 'CRIS', 'CRK', 'CRKN', 'CRMD', 'CRNT', 'CSCW', 'CSLT', 'CSPR', 'CTEK', 'CTIB', 'CTIC', 'CTK', 'CTMX', 'CTRM', 'CTSO', 'CTXR', 'CVE', 'CVGI', 'CWBR', 'CX', 'CXW', 'CYCC', 'CYCN', 'CYRN', 'CYTH', 'DARE', 'DBVT', 'DFFN', 'DGLY', 'DHC', 'DHT', 'DLPN', 'DNK', 'DNN', 'DNOW', 'DOGZ', 'DPW', 'DRH', 'DRRX', 'DRTT', 'DS', 'DSKE', 'DSS', 'DSSI', 'DSX', 'DTEA', 'DTSS', 'DUO', 'DVAX', 'DXF', 'DXLG', 'DYNT', 'EARS', 'EBON', 'EBR', 'ECOR', 'EDSA', 'EGY', 'EIGR', 'ELVT', 'ELYS', 'EMAN', 'EMKR', 'EMX', 'ENBL', 'ENDP', 'ENG', 'ENIA', 'ENIC', 'ENLC', 'ENSV', 'ENTX', 'ENVB', 'ENZ', 'EOLS', 'EQ', 'EQX', 'ERF', 'ERJ', 'ESGC', 'ESTE', 'ET', 'ETM', 'ETRN', 'ETTX', 'EURN', 'EVC', 'EVFM', 'EVGN', 'EVK', 'EVOK', 'EXK', 'EXPR', 'EXTR', 'EYEG', 'EYES', 'EZGO', 'EZPW', 'FAMI', 'FBIO', 'FBP', 'FENG', 'FI', 'FINV', 'FLDM', 'FLMN', 'FLNT', 'FLY', 'FORD', 'FPAY', 'FRBK', 'FRO', 'FRSX', 'FSM', 'FSP', 'FTEK', 'FTFT', 'FTK', 'FURY', 'GAU', 'GBS', 'GCI', 'GEL', 'GEN', 'GENE', 'GEO', 'GERN', 'GFI', 'GGAL', 'GGB', 'GHSI', 'GLBS', 'GLDG', 'GLG', 'GLOG', 'GLOP', 'GLUU', 'GLYC', 'GMBL', 'GMDA', 'GMLP', 'GNCA', 'GNK', 'GNLN', 'GNPX', 'GNUS', 'GNW', 'GOGL', 'GOL', 'GORO', 'GOSS', 'GOVX', 'GPL', 'GPMT', 'GPRO', 'GRIL', 'GRNQ', 'GSAT', 'GSKY', 'GSM', 'GSS', 'GSV', 'GTE', 'GTEC', 'GTT', 'GV', 'GVP', 'HAPP', 'HBM', 'HCDI', 'HCHC', 'HDSN', 'HEPA', 'HEXO', 'HGSH', 'HIL', 'HIMX', 'HJLI', 'HL', 'HLIT', 'HLX', 'HMHC', 'HMY', 'HNRG', 'HOFV', 'HOTH', 'HSTO', 'HT', 'HTBX', 'HUGE', 'HUSA', 'HUSN', 'HX', 'HYRE', 'IAG', 'IBIO', 'ICD', 'ICON', 'ID', 'IDEX', 'IDRA', 'IFMK', 'IFRX', 'IGC', 'IHT', 'IKT', 'IMAC', 'IMGN', 'IMMP', 'IMTE', 'IMV', 'INDO', 'INFI', 'ING', 'INN', 'INOD', 'INPX', 'INUV', 'IO', 'IPDN', 'IRIX', 'ISEE', 'ISIG', 'ISR', 'ITP', 'ITRM', 'ITUB', 'IVR', 'IZEA', 'JAGX', 'JE', 'JFIN', 'JFU', 'JG', 'JIH.W', 'JILL', 'JOB', 'JUPW', 'KALA', 'KBNT', 'KBSF', 'KDMN', 'KERN', 'KGC', 'KIN', 'KIQ', 'KMPH', 'KNDI', 'KODK', 'KOPN', 'KOS', 'KRKR', 'KRMD', 'KTRA', 'KUKE', 'KXIN', 'KZIA', 'LCI', 'LCTX', 'LEAF', 'LEE', 'LGHL', 'LIFE', 'LITB', 'LIVX', 'LIZI', 'LJPC', 'LKCO', 'LLIT', 'LLNW', 'LMFA', 'LMNL', 'LODE', 'LOMA', 'LPCN', 'LPTH', 'LPTX', 'LQDA', 'LSEA', 'LTBR', 'LTRPA', 'LX', 'LXRX', 'LYG', 'MACK', 'MARK', 'MBI', 'MBII', 'MBIO', 'MBRX', 'MBT', 'MCEP', 'MCF', 'MDGS', 'MDXG', 'MEIP', 'MESA', 'MESO', 'METX', 'MFA', 'MFG', 'MFGP', 'MFH', 'MGI', 'MGY', 'MHLD', 'MICT', 'MIN', 'MIND', 'MITO', 'MITT', 'MKD', 'MKGI', 'MLND', 'MLSS', 'MNKD', 'MOGO', 'MOGU', 'MOHO', 'MOSY', 'MOTS', 'MOXC', 'MPLN', 'MRC', 'MREO', 'MRIN', 'MRKR', 'MRO', 'MSN', 'MTA', 'MTC', 'MTL', 'MTNB', 'MTP', 'MTSL', 'MUFG', 'MUX', 'MVIS', 'MYSZ', 'MYT', 'NAK', 'NAKD', 'NAOV', 'NAT', 'NAVB', 'NBEV', 'NBRV', 'NBSE', 'NBY', 'NCMI', 'NCNA', 'NDRA', 'NEOS', 'NEPT', 'NERV', 'NES', 'NEW', 'NEX', 'NG', 'NGD', 'NGL', 'NH', 'NLY', 'NMCI', 'NMRK', 'NMTR', 'NNVC', 'NOK', 'NOVN', 'NR', 'NRZ', 'NSCO', 'NSPR', 'NTEC', 'NTN', 'NURO', 'NVCN', 'NVIV', 'NWG', 'NXE', 'NXTD', 'NYMT', 'OBLG', 'OBLN', 'OBSV', 'OCG', 'OCGN', 'OCSL', 'OCX', 'OEG', 'OGEN', 'OGI', 'OIBR.C', 'OII', 'OIIM', 'OIS', 'ONCT', 'ONCY', 'ONTX', 'OPGN', 'OPK', 'OPTN', 'OPTT', 'ORBC', 'ORC', 'ORMP', 'ORN', 'ORTX', 'OSMT', 'OSW', 'OTIC', 'OTLK', 'OVID', 'OXBR', 'OXLC', 'PAA', 'PAE', 'PAGP', 'PAVM', 'PAYS', 'PBF', 'PBI', 'PDSB', 'PED', 'PEI', 'PEIX', 'PFMT', 'PGEN', 'PGRE', 'PHAS', 'PHIO', 'PHUN', 'PIRS', 'PIXY', 'PLAG', 'PLG', 'PLIN', 'PLM', 'PLYA', 'PNNT', 'POAI', 'POWW', 'PPBT', 'PPR', 'PPSI', 'PPT', 'PROG', 'PRPO', 'PRQR', 'PRTK', 'PRTY', 'PSEC', 'PSTI', 'PSTV', 'PT', 'PTE', 'PTEN', 'PTMN', 'PTN', 'PULM', 'PUMP', 'PVL', 'PXLW', 'PXS', 'QD', 'QEP', 'QIWI', 'QLGN', 'QLI', 'QTNT', 'QTT', 'QUAD', 'QUOT', 'RAIL', 'RAVE', 'RBBN', 'RCON', 'RDHL', 'REED', 'REFR', 'REI', 'REPH', 'RES', 'RESN', 'RETO', 'RFP', 'RGLS', 'RGS', 'RHE', 'RIBT', 'RIG', 'RIGL', 'RKDA', 'RLH', 'RMED', 'RMTI', 'RNWK', 'RPAI', 'RPT', 'RRC', 'RRD', 'RTLR', 'RUHN', 'RWLK', 'RWT', 'RYAM', 'SALM', 'SAN', 'SAND', 'SB', 'SBBP', 'SBS', 'SCKT', 'SCOR', 'SCYX', 'SD', 'SDPI', 'SEAC', 'SEEL', 'SELB', 'SENS', 'SESN', 'SFET', 'SFL', 'SFT', 'SGBX', 'SGLB', 'SGOC', 'SHIP', 'SID', 'SIEN', 'SIF', 'SIFY', 'SILV', 'SINO', 'SINT', 'SIOX', 'SIRI', 'SLCA', 'SLDB', 'SLGG', 'SLRX', 'SLS', 'SM', 'SMFG', 'SMSI', 'SMTS', 'SMTX', 'SNCA', 'SNCR', 'SND', 'SNDE', 'SNDL', 'SNES', 'SNGX', 'SNMP', 'SNOA', 'SNR', 'SNSS', 'SOI', 'SOLO', 'SONM', 'SONN', 'SOS', 'SPCB', 'SPPI', 'SQFT', 'SQNS', 'SREV', 'SRGA', 'SSL', 'STAF', 'STCN', 'STON', 'STSA', 'SUP', 'SUPV', 'SVM', 'SVRA', 'SWN', 'SXC', 'SXTC', 'SYBX', 'SYN', 'SYPR', 'TACO', 'TALO', 'TANH', 'TAOP', 'TAST', 'TAT', 'TATT', 'TBLT', 'TCCO', 'TCDA', 'TCON', 'TEDU', 'TEF', 'TELL', 'TENX', 'TEO', 'TGA', 'TGB', 'TGC', 'THM', 'THMO', 'TK', 'TKAT', 'TKC', 'TLGT', 'TLMD', 'TLSA', 'TLYS', 'TMBR', 'TMDI', 'TMQ', 'TMST', 'TNAV', 'TNXP', 'TOPS', 'TOUR', 'TPRE', 'TRCH', 'TRIB', 'TRIT', 'TRST', 'TRUE', 'TRVG', 'TRVN', 'TRX', 'TRXC', 'TTI', 'TTNP', 'TTOO', 'TUSK', 'TV', 'TWI', 'TWO', 'TXMD', 'TYME', 'UAMY', 'UBX', 'UEC', 'UEPS', 'UGP', 'UMC', 'UONE', 'UONEK', 'URG', 'USAS', 'USAT', 'USEG', 'USIO', 'USWS', 'USX', 'UTSI', 'UUUU', 'UWMC', 'UXIN', 'VBIV', 'VBLT', 'VCNX', 'VEDL', 'VEON', 'VERB', 'VERO', 'VERU', 'VET', 'VGZ', 'VHC', 'VIOT', 'VIRI', 'VISL', 'VIV', 'VIVE', 'VKTX', 'VNTR', 'VRA', 'VRAY', 'VSTM', 'VTGN', 'VTNR', 'VTVT', 'VVOS', 'VXRT', 'VYGR', 'VYNE', 'WATT', 'WEI', 'WETF', 'WIMI', 'WISA', 'WIT', 'WKEY', 'WMC', 'WORX', 'WPRT', 'WPX', 'WRAP', 'WRN', 'WSR', 'WTER', 'WTI', 'WTRH', 'WTTR', 'WVE', 'WWR', 'XAIR', 'XAN', 'XBIO', 'XCUR', 'XELA', 'XELB', 'XERS', 'XNET', 'XPL', 'XSPA', 'XXII', 'YCBD', 'YGYI', 'YJ', 'YPF', 'YRCW', 'YTRA', 'YVR', 'ZAGG', 'ZIOP', 'ZIXI', 'ZKIN', 'ZNGA', 'ZOM', 'ZSAN', 'ZVO', 'ZYNE'] def get_name(_ticker): try: database[_ticker] = yf.Ticker(_ticker).info['longName'] except: WRONG_TICKERS.append(_ticker) pass return(database) number_of_cpu = joblib.cpu_count() delayed_funcs = [delayed(get_name)(_ticker) for _ticker in stocks] parallel_pool = Parallel(n_jobs=number_of_cpu,prefer="threads") # processes threads parallel_pool(delayed_funcs) OUTPUT: Note that depending on your computer, it could be faster withe 'processes' instead of 'threads'. You have to test to know it. Edit : When re-running the same code, everything is in 404 ERROR. Bust be a problem with yfinance Edit 2 : It's okay again...
How to get subsequent keys from a dictionary in python?
so I'm working on this referencing problem in which I need to get a key from a dictionary and its child keys(if any) for example, I have a list of dictionary keys as such: dict_keys(['1.', '1.1', '1.2', '1.2.1', '1.2.2', '1.2.2(a)', '1.2.2(b)', '1.2.2(c)', '1.2.2(d)', '1.2.3', '1.2.4', '1.2.5', '1.2.6', '1.2.7', '1.2.8', '1.2.9', '1.2.10', '1.2.11', '2.', '2.1', '3.', '3.1', '3.1.1', '3.1.2', '3.2', '3.3', '3.4', '3.5', '3.5.1', '3.5.2', '3.5.2(a)', '3.5.2(b)', '3.5.2(c)', '3.6', '3.7', '3.8', '3.9', '3.9.1', '3.9.2', '3.9.3', '3.10', '3.11', '3.11.1', '3.11.2', '4.', '4.1', '4.1.1', '4.1.2', '4.1.3', '4.1.4', '4.1.5', '4.1.6', '4.1.7', '4.1.8', '4.1.9', '4.1.10', '4.2', '5.', '5.1', '5.1.1', '5.1.2', '5.1.3', '5.1.4', '5.1.4(a)', '5.1.4(b)', '5.1.4(c)', '5.1.4(d)', '5.1.4(e)', '5.1.4(f)', '5.1.4(g)', '5.1.4(h)', '5.1.4(i)', '5.1.4(j)', '5.1.4(k)', '5.2', '5.3', '5.4', '6.', '6.1', '6.2', '7.', '7.1', '7.2', '7.3', '7.3.1', '7.3.2', '7.3.3', '7.4', '7.5', '7.6', '7.6.1', '7.6.2', '7.6.3', '7.6.4', '8.', '8.1', '8.2', '8.2.1', '8.2.2', '8.2.3', '8.2.4', '8.2.5', '8.2.6', '8.2.7', '8.3', '8.3.1', '8.3.2', '8.3.3', '8.3.4', '8.3.5', '8.3.6', '8.4', '9.', '9.1', '9.1.1', '9.1.2', '9.2', '9.3', '9.4', '9.5', '10.', '10.1', '10.2', '10.3', '10.4', '11.', '11.1', '11.2', '11.2.1', '11.2.2', '11.2.3', '11.3', '11.4', '11.5', '11.6', '11.7', '11.8', '11.9', '11.10', '12.', '12.1', '12.2', '12.2.1', '12.2.2', '12.3', '12.4', '12.4.1', '12.4.2', '12.4.3', '12.5', '12.6', '12.7', '12.8', '12.9', '12.10', '12.11', '13.', '13.1', '13.2', '13.3', '13.3.1', '13.3.2', '13.3.3', '13.3.4', '13.4', '13.4.1', '13.4.2', '13.4.3', '14.', '14.1', '14.2', '14.3', '15.', '15.1', '15.2', '15.2.1', '15.2.2', '15.2.3', '15.2.4', '16.', '16.1', '16.2', '16.3', '16.4', '16.5', '16.6', '16.7', '16.7.1', '16.7.2', '17.', '17.1', '17.1.1', '17.1.2', '17.1.3', '17.1.4', '17.2', '17.2.1', '17.2.2', '17.2.3', '17.2.4', '17.3', '17.4', '17.4.1', '17.4.2', '17.5', '17.5.1', '17.5.2', '17.5.3', '18.', '18.1', '18.2', '18.2.1', '18.2.1(a)', '18.2.1(b)', '18.2.1(c)', '18.2.2', '18.2.3', '18.2.4', '18.2.5', '18.2.5(a)', '18.2.5(b)', '18.2.6', '18.2.7', '19.', '19.1', '19.2', '19.3', '19.4', '20.', '21.', '21.1', '21.2', '21.3', '21.4', '22.', '22.1', '22.1.1', '22.1.2', '22.1.3', '22.1.4', '22.2', '22.2.1', '22.2.2', '22.3', '22.4', '22.5', '22.6', '22.6.1', '22.6.2', '22.7', '23.', '23.1', '23.1.1', '23.1.1(a)', '23.1.1(b)', '23.1.1(c)', '23.1.2', '23.1.3', '23.1.3(a)', '23.1.3(b)', '23.1.4', '23.2', '23.2.1', '23.2.2', '23.3', '23.4', '23.5', '24.', '24.1', '24.1.1', '24.1.2', '24.1.3', '24.1.4', '24.1.5', '24.1.6', '24.2', '24.3', '25.', '25.1', '25.1.1', '25.1.2', '25.1.3', '25.1.4', '25.1.5', '25.1.6', '25.2', '25.2.1', '25.2.2', '25.2.3', '25.2.4', '25.2.5', '25.3', '26.', '27.', '27.1', '27.2', '27.2.1', '27.2.2', '27.2.3', '27.3', '28.', '28.1', '28.2', '28.2.1', '28.2.2', '28.2.3', '28.2.4', '28.2.5', '28.3', '29.', '29.1', '29.1.1', '29.1.2', '29.1.3', '29.2', '30.', '30.1', '30.2', '30.2.1', '30.2.2', '30.2.3', '30.3', '30.4', '31.', '31.1', '31.2', '31.3', '31.4', '31.5', '31.5.1', '31.5.2', '31.6', '31.7', '31.8', '31.8.1', '31.8.2', '31.8.3', '31.9', '31.10', '31.11']) in this, if I have something referred in key 3.1, I also want to extract the data for keys 3.1.1 and 3.1.2(child keys). so for this I used str.startwith() method which fails in cases as such. It will not only return me the child clauses but also other clauses like 3.10, 3.11... which also start with 3.1, so this will result in some false positives and leakages. The caveat however being that I don't know for sure that what numbering system will the user be using in the agreement. for example: it could be anything like: ['(1.)', '(1.)a.', '(2.)'] or ['1.', '1.1.', '1.1.1.', '1.1.1.a',] or ['1.', '1.1', '1.1.1', '1.1.1a'] so I'm trying to figure out a function using which I can cover all these ways. How can I do that? Thanks in advance! :)
You're much more familiar with your dataset and how robust you need to be, but I set that set of dict_keys to keys and used the following: if key.count('.') < 2: child_keys = [k for k in keys if k.startswith(key + '.')] else: child_keys = [k for k in keys if k.startswith(key + '(')] If key = '1.2.2', then child_keys = ['1.2.2(a)', '1.2.2(b)', '1.2.2(c)', '1.2.2(d)']. If key = '3.1', then child_keys = ['3.1.1', '3.1.2'].
def get_subsequent_keys(key): subsequent_keys = [] total_keys = structure.keys() try: if key[-1] == '.' or key[-1] == ')': if key in total_keys: pass elif key[:-1] in total_keys: subsequent_keys.append(key[:-1]) subsequent_keys += [x for x in total_keys if x.startswith(key)] else: key_pat = fr"({key}[a-z]+)" subsequent_keys.append(key) subsequent_keys += [x for x in total_keys if x.startswith(key + '.')] subsequent_keys += [x for x in total_keys if x.startswith(key + '(')] subsequent_keys += re.findall(key_pat, ' '.join(total_keys)) except Exception as exception: logger.debug(exception) finally: return subsequent_keys So I used this way to cover all the possible cases. The problem with my use case is that I don't really have a definite way by which the user numbers the clauses in the agreement. Hence I needed a generic function to cover all ground. Also, thanks to Ava and Hayley for their help! :D
How do i replace a specific combination of integer and string?
I'm trying to replace a python list with a deck of cards with symbols. I have tried using Pythons replace function, but i assume the best solution for this is probably based on regular expression replacement. The desired result would be this: "Ah" => "A♥" "5h" => "5♥" etc. Currently the list features items like this: [Player name], [Player wallet], [1st player card], [2nd player card] This could be i.e.: ["Don Johnson", 100, "Ks", "5d"] ["Davey Jones", 100, "4c", "3h"] Any help for this would be greatly appreciated. Thanks. (Edited for clarification on request - Thanks for all the input so far!)
Here, we can simply use four simple expressions and make that replacement that we wish: ([AKJQ0-9]{1,2})h ([AKJQ0-9]{1,2})d and similarly the other two. Demo # coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"([AKJQ0-9]{1,2})h" test_str = ("Ah\n" "10h") subst = "\\1♥" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 0, re.MULTILINE) if result: print (result) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
If you had just a list of cards then it would probably look something like this: cards = ['2h', '2s', '2c', '2d', '3h', '3s', '3c', '3d', '4h', '4s', '4c', '4d', '5h', '5s', '5c', '5d', '6h', '6s', '6c', '6d', '7h', '7s', '7c', '7d', '8h', '8s', '8c', '8d', '9h', '9s', '9c', '9d', '10h', '10s', '10c', '10d', 'Ah', 'As', 'Ac', 'Ad', 'Kh', 'Ks', 'Kc', 'Kd', 'Jh', 'Js', 'Jc', 'Jd', 'Qh', 'Qs', 'Qc', 'Qd'] If so then just use a dict and a comprehension: suits = {'h': '♥', 's': '♠', 'c': '♣', 'd': '♦'} new_cards = [''.join(rank)+suits[suit] for *rank, suit in cards] Output for this is: ['2♥', '2♠', '2♣', '2♦', '3♥', '3♠', '3♣', '3♦', '4♥', '4♠', '4♣', '4♦', '5♥', '5♠', '5♣', '5♦', '6♥', '6♠', '6♣', '6♦', '7♥', '7♠', '7♣', '7♦', '8♥', '8♠', '8♣', '8♦', '9♥', '9♠', '9♣', '9♦', '10♥', '10♠', '10♣', '10♦', 'A♥', 'A♠', 'A♣', 'A♦', 'K♥', 'K♠', 'K♣', 'K♦', 'J♥', 'J♠', 'J♣', 'J♦', 'Q♥', 'Q♠', 'Q♣', 'Q♦'] For your solution you could define a function that corrects the card: def fix_card(card): suits = {'h': '♥', 's': '♠', 'c': '♣', 'd': '♦'} *rank, suit = card return ''.join(rank)+suits[suit] Then just use it like this: player = ["Don Johnson", 100, "Ks", "5d"] player[2] = fix_card(player[2]) player[3] = fix_card(player[3]) print(player) #["Don Johnson", 100, "K♣", "5♦"]
No, regex is not needed for a simple replacement like this. Just use str.replace: >>> cards = ['Ah', '5h'] >>> [s.replace('h', '♥') for s in cards] ['A♥', '5♥']
'set' object has no attribute 'rstrip'
from random import randint import os path = os.path.abspath('') dosya = open((path + "\\asaldb.txt"), "r") asalsayilar = dosya.readlines() mindeger = int(input("Minumum Değer Kaç ? ")) maxdeger = int(input("Maximum Değer Kaç ? ")) min=0 max=0 minasallar = [] maxasallar = [] ortakasal = [] ortakasal2 = [] def maxasal(): global max if maxdeger>=int(asalsayilar[max]): maxasallar.append((asalsayilar[max]).rstrip('\n')) max=max+1 maxasal() def minasal(): global min if int(asalsayilar[min])>=mindeger: try: (asalsayilar[max]).rstrip('\n') minasallar.append((asalsayilar[min]).rstrip('\n')) min=min+1 minasal() except: print("Maximum Asallar :")""" maxasal() else: min=min+1 minasal() minasal() def common_member(a, b): a_set = set(a) b_set = set(b) if (a_set & b_set): ortakasal.append((a_set & b_set)) else: print("No common elements") common_member(minasallar, maxasallar) ortakasal.sort() print((ortakasal[0]).rstrip('{ }')) Traceback: Traceback (most recent call last): File "asal.py", line 54, in <module> print((ortakasal[0]).rstrip('{ }')) AttributeError: 'set' object has no attribute 'rstrip' If I do print (asalortak), it becomes like this: [{'83', '97', '53', '79', '89', '67', '61', '73', '59' }] If I do print (asalortak [0]), it looks like this: {'83', '97', '53', '79', '89', '67', '61', '73', '59', '71'}, but I need to separate my numbers.
I do print (asalortak [0]), it looks like this: {'83', '97', '53', '79', '89', '67', '61', '73', '59', '71'} that's because asalortak [0] is a python set which is confirmed by this error. print((ortakasal[0]).rstrip('{ }')) AttributeError: 'set' object has no attribute 'rstrip' The elements are already "separated". Just iterate on the elements to print them: for e in ortakasal[0]: print(e) in a set, the order isn't stable/fixed. Used sorted to generate a sorted list out of it (sorted alpha, not numerical since numbers are strings) convert to list: lst = list(ortakasal[0]) convert to sorted list: lst = sorted(ortakasal[0]) you can use random.sample(ortakasal[0],2) to select 2 elements at random.
Move item from list to another one for poker card game Python
I'm trying to make poker game in Python. In the while fuction I want to move the used cards in a separate(used cards) list. The problem is sometimes when I print the hand I can get duplicates. Something is wrong with my sorting strategy and I don't know what. Can you help me? import random deck = ['AS', 'KS', 'QS', 'JS', '10S', '9S', '8S', '7S', '6S', '5S', '4S', '3S', '2S',\ 'AD', 'KD', 'QD', 'JD', '10D', '9D', '8D', '7D', '6D', '5D', '4D', '3D', '2D',\ 'AC', 'KC', 'QC', 'JC', '10C', '9C', '8C', '7C', '6C', '5C', '4C', '3C', '2C',\ 'AH', 'KH', 'QH', 'JH', '10H', '9H', '8H', '7H', '6H', '5H', '4H', '3H', '2H'] used = [] p1 = [] p2 = [] a = 0 while (a < 2): drawn_card = random.choice(deck) deck.append(drawn_card) deck = [f for f in deck if f not in used] p1.append(drawn_card) a+=1
Well the random choice is not guaranteed to be unique, thus when you do: drawn_card = random.choice(deck) .. p1.append(drawn_card) you may end up having duplicates (that explains that you some time see duplicates and some not). Check if drawn_card is in the list first and if not, then append. That way you won't have duplicates. In code you could it like this: if drawn_card not in p1: p1.append(drawn_card) Or, as Rory Daulton said: If you are allowed, you could shuffle the entire deck, then remove consecutive items from that list.
you need to compare the random card with "p1" not with "deck": import random deck = ['AS', 'KS', 'QS', 'JS', '10S', '9S', '8S', '7S', '6S', '5S', '4S', '3S', '2S',\ 'AD', 'KD', 'QD', 'JD', '10D', '9D', '8D', '7D', '6D', '5D', '4D', '3D', '2D',\ 'AC', 'KC', 'QC', 'JC', '10C', '9C', '8C', '7C', '6C', '5C', '4C', '3C', '2C',\ 'AH', 'KH', 'QH', 'JH', '10H', '9H', '8H', '7H', '6H', '5H', '4H', '3H', '2H'] used = [] p1 = [] a = 0 while (a < 2): drawn_card = random.choice(deck) print(drawn_card) if drawn_card not in p1: p1.append(drawn_card) a += 1 continue print (p1)