【问题标题】:pythonic way to identify names in url and match it with an existing set of namespythonic 方法来识别 url 中的名称并将其与现有的一组名称匹配
【发布时间】:2020-07-21 19:09:25
【问题描述】:

您好,这是一个我想解决的问题,但我被卡住了。

给定一个 url 列表,我想执行以下操作:

  1. 提取网址中的名称
  2. 将从 url 中找到的名称与现有名称的字典相匹配
  3. 有 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


【解决方案1】:

嗯...如果我得到了你想要做的正确...这里是应该工作的东西


for link in links_list:
    link_split = link.split('/')
    name_list = link_split[2].split('-')     # makes from chris-brown-xx => chrisbrownxx
    name = ""
    for part in name:
        name + part
    for (key, value) in existing_names:    # check if the name is in the list
        for name_x in value:
            name_x = # same as I did with name_list, but this time with " "
            if name_x in name.lower():
                # append it to new_names_found

(抱歉,我正在手机上输入此内容,但希望对您有所帮助:))

(或者,您可以尝试查看它是否包含文本的两个部分......但这样会失败 -> “Luke Luk”并在“Luke O'Niel”上检查它)......有很多问题

【讨论】:

    【解决方案2】:

    让你开始吧,这里是制作这个程序的步骤:

    1. 创建一个 for 来查看每个单独的 url,并使用 split('/') 函数将每个 url 分解为一个列表并在该列表中搜索 2 值。
    2. 然后您可以使用另一个for 循环来遍历existing_names 字典的键和值。在该循环中包含一个 if 语句,该语句将您提取的名称与存在的名称进行比较。
    3. 然后将这些值添加到所需的字典或列表中。

    【讨论】:

      【解决方案3】:

      对于在 url 中识别名称的第一部分,您可以执行以下操作:

      urls = [i for i in urls if 'name' in i]
      
      found_celeb = {}
      for url in urls:
          link_split = url.split('=')[-1].split(',')[-1]
          celeb_name = ' '.join(link_split)
          found_celeb[url] = celeb_name
      

      【讨论】:

        猜你喜欢
        • 2021-12-08
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多