基于anoth中匹配项的元组排序列表

2024-06-16 10:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试按匹配项对两个元组列表进行排序。元组包含从体育博彩网站上搜集的数据。我已经编写了一些代码来匹配每个列表中的条目,并将它们附加到一个新的列表中。我遇到的问题是找到一个排序函数,可以根据不完美的匹配进行搜索,例如,可能有一个额外的空白在一个名字或有可能由缩短的球队名称,如'北昆士兰牛仔'在sportsbet\u列表,而不是北昆士兰牛仔在列表\u结局。请参见以下列表:

list_finale = [[('Canterbury Bulldogs ', '3.25'), ('South Sydney Rabbitohs', '1.34')], [('Parramatta Eels ', '1.79'), ('Wests Tigers', '2.02')], [('Melbourne Storm ', '1.90'), ('Sydney Roosters', '1.90')], [('Gold Coast Titans ', '1.86'), ('Newcastle Knights', '1.94')], [('New Zealand Warriors ', '1.39'), ('North Queensland Cowboys', '2.95')], [('Cronulla Sharks ', '1.68'), ('Penrith Panthers', '2.18')], [('St. George Illawarra Dragons ', '1.45'), ('Manly Sea Eagles', '2.74')], [('Canberra Raiders ', '1.63'), ('Brisbane Broncos', '2.26')]]


sportsbet_list = [[('Cronulla Sharks', '1.64'), ('Penrith Panthers', '2.27')], [('Canterbury Bulldogs', '3.30'), ('South Sydney Rabbitohs', '1.33')], [('Melbourne Storm', '1.90'), ('Sydney Roosters', '1.90')], [('New Zealand Warriors', '1.40'), ('Nth Queensland Cowboys', '2.90')], [('St George Illawarra Dragons', '1.45'), ('Manly Sea Eagles', '2.75')], [('Gold Coast Titans', '1.85'), ('Newcastle Knights', '1.95')], [('Canberra Raiders', '1.60'), ('Brisbane Broncos', '2.30')], [('Parramatta Eels', '1.90'), ('Wests Tigers', '1.90')], [('Sydney Roosters', '1.35'), ('St George Illawarra Dragons', '3.20')], [('Melbourne Storm', '1.25'), ('New Zealand Warriors', '4.00')], [('Canterbury Bulldogs', '1.56'), ('Nth Queensland Cowboys', '2.40')], [('Penrith Panthers', '2.20'), ('South Sydney Rabbitohs', '1.67')], [('Wests Tigers', '1.67'), ('Gold Coast Titans', '2.20')], [('Brisbane Broncos', '1.70'), ('Cronulla Sharks', '2.15')], [('Manly Sea Eagles', '1.85'), ('Canberra Raiders', '1.95')], [('Newcastle Knights', '1.80'), ('Parramatta Eels', '2.00')]]

我目前用于对这些列表进行排序的代码如下所示:

list_n = []

list_n1 = []

for a in sportsbet_list:
    for b in list_finale:
        if b[0][0] == a[0][0] and b[1][0] == a[1][0]:
            list_n.append(a)
            list_n1.append(b)

这只适用于团队名称完全相同的情况

基本上,我需要一个匹配函数来接受b[0][0] == a[0][0] and b[1][0] == a[1][0]:如果它们85%相似或类似的话

我是一个非常新的编码,所以任何建议或帮助将不胜感激


Tags: 列表排序listsydneysouthstormeelsmelbourne
1条回答
网友
1楼 · 发布于 2024-06-16 10:28:35

你可以走几条路。第一种可能是更严格的匹配解决方案,而第二种是模糊的

  • 更严格的方法(规范化输入,然后进行常规排序)

这个解决方案可能适合您的需要,也可能不适合您的需要,但基本上是这样的:您需要使这些名称尽可能接近所需的状态。下面的示例很有希望说明您可能会做什么:

team_bet_list = [('Canterbury Bulldogs ', '3.25'), ('South Sydney Rabbitohs', '1.34')] 

def normalize_team(item):
    substitutions = {
      'north': 'nth',
      'south': 'sth',  # etc
    }
    words = [word.lower() for word in item[0].strip()]
    return (words, item[1])  # returning a new tuple   you might even want to return the original non-normalized value, if that's important to you

normalized_values = [normalize(pair) for pair in team_bet_list]

# Now you should be able to sort these, but it'll take some experimentation to find the best normalization approach
  • 第二种选择是使用模糊匹配。我想到了两个。首先是使用levenstien距离,它基本上告诉您任意两个字符串的距离,基于需要发生的字符更改的数量(e、 g.绳索->;船的距离为3({r->;b、 p->;a、 e->;t} 是的。使用这个,您可以尝试在每个列表中找到最接近的匹配,并假设它是正确的值。或者,你可以试试像fuzzyfuzzy这样的图书馆。这以前对我来说很管用。见https://github.com/seatgeek/fuzzywuzzy

相关问题 更多 >