【发布时间】:2020-07-21 19:09:25
【问题描述】:
您好,这是一个我想解决的问题,但我被卡住了。
给定一个 url 列表,我想执行以下操作:
- 提取网址中的名称
- 将从 url 中找到的名称与现有名称的字典相匹配
- 有 1 个包含找到的所有名称的字典,将找到的名称拆分为 2 个单独的字典,其中一个与在字典中找到的名称相关联,另一个与未找到的名称相关联
示例:
INPUT :
urls = ['www.twitter.com/users/aoba-joshi/$#fsd=43r',
'www.twitter.com/users/chrisbrown-e2/#4f=34ds',
'www.facebook.com/celebrity/neil-degrasse-tyson',
'www.instagram.com/actor-nelson-bigetti']
# the key is the ID associated to the names, and the values are all the potential names
existing_names = {1 : ['chris brown', 'chrisbrown', 'Brown Chris', 'brownchris'] ,
2 : ['nelson bigetti', 'bigetti nelson', 'nelsonbigetti', 'bigettinelson'],
3 : ['neil degrasse tyson', 'tyson neil degreasse', 'tysonneildegrasse', 'neildegrassetyson']}
OUTPUT :
# names_found will be a dictionary with the key as the URL and the values as the found name
names_found = {'www.twitter.com/users/aoba-joshi/$#fsd=43r' : 'aoba joshi',
'www.twitter.com/users/chrisbrown-e2/#4f=34ds' : 'chris brown',
'www.facebook.com/celebrity/neil-degrasse-tyson' : 'neil degrasse tyson',
'www.instagram.com/actor-nelson-bigetti' : 'nelson bigetti'}
# existing_names_found is a dictionary where the keys are the found name, and the values are the corresponding list of names in the existing names dictionary
existing_names_found = {'chris brown' : ['chris brown', 'chrisbrown', 'Brown Chris', 'brownchris'],
'neil degrasse tyson' : ['neil degrasse tyson', 'tyson neil degreasse', 'tysonneildegrasse', 'neildegrassetyson'],
'nelson bigetti' : ['nelson bigetti', 'bigetti nelson', 'nelsonbigetti', 'bigettinelson']}
# new_names_found is a dictionary with the keys as the new name found, and the values as the url associated to the new found name
new_names_found = {'aoba joshi' : 'www.twitter.com/users/aoba-joshi/$#fsd=43r'}
【问题讨论】:
-
很好,您已经添加了输入和预期输出,但忘记添加您迄今为止尝试过的代码。
标签: python arrays list dictionary parsing