I want to build URL's responding to a specific format, the URL's are made of a concatenation of French regions and departments (subdivisions of regions) codes, such as this one : https://www.resultats-elections.interieur.gouv.fr/telechargements/PR2022/resultatsT1/027/021/
I have set two dictionaries, one for the regions and one for the departments codes associated.
regions = {
'centre_loire': '024',
'bourgogne': '027',
'normandie': '028',
'grand_est': '044',
'pays_loire': '052',
'bretagne': '053',
'aquitaine': '075',
}
departements = {
'centre_loire': ['018','028','036','037','041','045'],
'bourgogne': ['021','025','039','058','070','071','089','090'],
'normandie': ['014','027','050','061','076'],
'grand_est': ['008','010','051','052','054','055','057','067','068','088'],
'pays_loire': ['044','049','053','072','085'],
'bretagne': ['022','029','035','056'],
'aquitaine': ['016','017','019','023','024','033','040','047','064','079','086','087'],
}
The idea is to iterate through those two dictionaries to build URL's but the way I have arranged the for loop associates all the departments with all the regions, even those that have nothing to do together.
regs = []
urls_final = []
for i in regions.values():
regs = (url_base tour '/' str(i) '/')
for key, values in departements.items():
for value in values:
deps_result = (regs str(value) '/' str(value) '/')
urls_final.append(deps_result)
print(urls_final)
For example, for the "bourgogne" region, the URL's created should contain only the 8 departments codes corresponding to the "bourgogne" region.
CodePudding user response:
Use the key from the regions
dict to get the list values of the departements
dict.
regions = {
'centre_loire': '024',
'bourgogne': '027',
'normandie': '028',
'grand_est': '044',
'pays_loire': '052',
'bretagne': '053',
'aquitaine': '075',
}
departements = {
'centre_loire': ['018','028','036','037','041','045'],
'bourgogne': ['021','025','039','058','070','071','089','090'],
'normandie': ['014','027','050','061','076'],
'grand_est': ['008','010','051','052','054','055','057','067','068','088'],
'pays_loire': ['044','049','053','072','085'],
'bretagne': ['022','029','035','056'],
'aquitaine': ['016','017','019','023','024','033','040','047','064','079','086','087'],
}
url_base = "https://whatever.com"
urls = []
for reg, reg_code in regions.items():
for dep_code in departements[reg]:
urls.append(f"{url_base}/{reg_code}/{dep_code}")
from pprint import pprint
pprint(urls)
output
['https://whatever.com/024/018',
'https://whatever.com/024/028',
'https://whatever.com/024/036',
'https://whatever.com/024/037',
'https://whatever.com/024/041',
'https://whatever.com/024/045',
'https://whatever.com/027/021',
'https://whatever.com/027/025',
'https://whatever.com/027/039',
'https://whatever.com/027/058',
'https://whatever.com/027/070',
'https://whatever.com/027/071',
'https://whatever.com/027/089',
'https://whatever.com/027/090',
'https://whatever.com/028/014',
'https://whatever.com/028/027',
'https://whatever.com/028/050',
'https://whatever.com/028/061',
'https://whatever.com/028/076',
'https://whatever.com/044/008',
'https://whatever.com/044/010',
'https://whatever.com/044/051',
'https://whatever.com/044/052',
'https://whatever.com/044/054',
'https://whatever.com/044/055',
'https://whatever.com/044/057',
'https://whatever.com/044/067',
'https://whatever.com/044/068',
'https://whatever.com/044/088',
'https://whatever.com/052/044',
'https://whatever.com/052/049',
'https://whatever.com/052/053',
'https://whatever.com/052/072',
'https://whatever.com/052/085',
'https://whatever.com/053/022',
'https://whatever.com/053/029',
'https://whatever.com/053/035',
'https://whatever.com/053/056',
'https://whatever.com/075/016',
'https://whatever.com/075/017',
'https://whatever.com/075/019',
'https://whatever.com/075/023',
'https://whatever.com/075/024',
'https://whatever.com/075/033',
'https://whatever.com/075/040',
'https://whatever.com/075/047',
'https://whatever.com/075/064',
'https://whatever.com/075/079',
'https://whatever.com/075/086',
'https://whatever.com/075/087']
CodePudding user response:
base = "www.domain.com/{}/{}"
urls = [base.format(dept, subdept) for region, dept in regions.items() for subdept in departments[region]]
print(urls)
Output:
['www.domain.com/024/018', 'www.domain.com/024/028', 'www.domain.com/024/036', 'www.domain.com/024/037', 'www.domain.com/024/041', 'www.domain.com/024/045', 'www.domain.com/027/021', 'www.domain.com/027/025', 'www.domain.com/027/039', 'www.domain.com/027/058', 'www.domain.com/027/070', 'www.domain.com/027/071', 'www.domain.com/027/089', 'www.domain.com/027/090', 'www.domain.com/028/014', 'www.domain.com/028/027', 'www.domain.com/028/050', 'www.domain.com/028/061', 'www.domain.com/028/076', 'www.domain.com/044/008', 'www.domain.com/044/010', 'www.domain.com/044/051', 'www.domain.com/044/052', 'www.domain.com/044/054', 'www.domain.com/044/055', 'www.domain.com/044/057', 'www.domain.com/044/067', 'www.domain.com/044/068', 'www.domain.com/044/088', 'www.domain.com/052/044', 'www.domain.com/052/049', 'www.domain.com/052/053', 'www.domain.com/052/072', 'www.domain.com/052/085', 'www.domain.com/053/022', 'www.domain.com/053/029', 'www.domain.com/053/035', 'www.domain.com/053/056', 'www.domain.com/075/016', 'www.domain.com/075/017', 'www.domain.com/075/019', 'www.domain.com/075/023', 'www.domain.com/075/024', 'www.domain.com/075/033', 'www.domain.com/075/040', 'www.domain.com/075/047', 'www.domain.com/075/064', 'www.domain.com/075/079', 'www.domain.com/075/086', 'www.domain.com/075/087']
>>>
Note: I've renamed your departements
dictionary to departments
.