【问题标题】:Python string matching to a string in a list on indices and return a value in the list based on most matchesPython 字符串与索引列表中的字符串匹配,并根据大多数匹配返回列表中的值
【发布时间】:2023-01-20 06:30:22
【问题描述】:

我将一个字符串参数传递给一个函数,我希望它与列表中的项目尽可能匹配。这里增加的复杂层是列表中的参数和项目最终都会被“.”分割。我需要查看字符串/值是否按索引匹配。

我不擅长递归,所以我正在尝试使用enumerate()。

在展示我所做的之前展示我想要的更容易:

hero_episode_list = ["Batman.1.1.2.3.5.6", "Batman.1.2.1.1", "Batman.1.3.1.4", 
                     "Batman.1.1.2.3.4", "Batman.1.2.2.1.3", "Superman.1.2.1.3.4", "Superman.1.3.2.1", "Superman.1.1.2.4"]


def get_hero_match(hero):
  if hero in hero_episode_list:  # If the argument matches an item in the list EXACTLY, return the item.
    return hero
  else:
    hero_split = hero.split(".")
    for ep in hero_episode_list:
      ep_split = ep.split(".")
      print(f"Function arg: {hero_split}")
      print(f"List    item: {ep_split}")
      print("------")

get_hero_match("Batman.1.2.1.1.3")

Output:

Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Batman', '1', '1', '2', '3', '5', '6']
------
Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Batman', '1', '2', '1', '1']  <- should return this since it has the most matches
------
Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Batman', '1', '3', '1', '4']
------
Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Batman', '1', '1', '2', '3', '4']
------
Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Batman', '1', '2', '2', '1', '3']
------
Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Superman', '1', '2', '1', '3', '4']
------
Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Superman', '1', '3', '2', '1']
------
Function arg: ['Batman', '1', '2', '1', '1', '3']
List    item: ['Superman', '1', '1', '2', '4']
------

这是我正在尝试的:

hero_episode_list = ["Batman.1.1.2.3.5.6", "Batman.1.2.1.1", "Batman.1.3.1.4", 
                     "Batman.1.1.2.3.4", "Batman.1.2.2.1.3", "Superman.1.2.1.3.4", "Superman.1.3.2.1",
                     "Superman.1.1.2.4"]

def get_hero_match(hero):
  if hero in hero_episode_list:  # If the argument matches an item in the list EXACTLY, return the item.
    return hero
  else:
    hero_split = hero.split(".")
    ep_split = [ep.split(".") for ep in hero_episode_list]
    for item in ep_split:
      for count, (h, e) in enumerate(zip(hero_split, item)):
        if h == e:
          print(count, h, e)

get_hero_match("Batman.1.2.1.1.3")

Output:

0 Batman Batman
1 1 1
0 Batman Batman  <-- should return this one
1 1 1
2 2 2
3 1 1
4 1 1
0 Batman Batman
1 1 1
3 1 1
0 Batman Batman
1 1 1
0 Batman Batman <- don't know what this one's doing
1 1 1
2 2 2
4 1 1
5 3 3
1 1 1
2 2 2
3 1 1
1 1 1
4 1 1
1 1 1

如何使用枚举获得最高匹配的“计数”值?我想用它来返回列表中的值,因为它按索引具有最多的匹配项。

【问题讨论】:

    标签: python string list enumeration indices


    【解决方案1】:

    我做到了,所以它只能匹配,如果英雄是相同的,例如:超人。如果没有匹配项,则匹配项将是列表的第一个元素。

    def get_hero_match(hero):
        if hero in hero_episode_list:
            return hero
        else:
            hero_split = hero.split(".")
            #Set the default best match to the first element of the hero list
            max_matches = [0, hero_episode_list[0]]
            for ep in hero_episode_list:
                ep_split = ep.split(".")
                
                #If the hero starts with something other than the input, then skip to the next one
                if ep_split[0] != hero_split[0]: continue
    
                number_of_matches = 0
                #Loop through both of the separated versions
                for ep_part, hero_part in zip(ep_split, hero_split):
                    print(ep_part, hero_part)
                    #In case of a match, add True (1) to the current number of matches                     
                    if ep_part == hero_part:
                        number_of_matches += 1
                    else: break
                else:
                    if number_of_matches > max_matches[0]: #If the number of matches bigger than the previous then set it to the max
                        max_matches = [number_of_matches, ep]
                print(f"Function arg: {hero_split}")
                print(f"List    item: {ep_split}")
                print("------")
        print(max_matches[1]) #Print out the best match
    

    【讨论】:

    • 感谢这个 TLeo。这适用于一些示例,但我很好奇您是否有关于一旦索引没有匹配项就可以停止迭代的建议。前任。 get_hero_match("Batman.1.1.2.3.4.6") 返回“Batman.1.1.2.3.5.6”作为最佳匹配,但应返回“Batman.1.1.2.3.4”。当它到达索引 [5]​​ 并发现“4”和“5”不匹配时,它应该停在那里。但它返回“Batman.1.1.2.3.5.6”,因为它继续前进并看到两个字符串索引 [-1] 都是 6,所以它向匹配项添加 True(1)。我没有添加到原始帖子的错误,所以如果没有回复,请理解。
    猜你喜欢
    • 2017-01-09
    • 2023-03-07
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多