【问题标题】:Comparing a list of lists with a list to find similar numbers将列表列表与列表进行比较以找到相似的数字
【发布时间】:2014-02-06 00:06:29
【问题描述】:

这是我尝试过的:

my_tickets = [ [ 7, 17, 37, 19, 23, 43],
           [ 7, 2, 13, 41, 31, 43],
           [ 2, 5, 7, 11, 13, 17],
           [13, 17, 37, 19, 23, 43] ]

def lotto_matches(xs, my_tickets):
   xi = 0 # could be used to pick specific things from lists
   yi = 0 # could be used to pick specific things from lists
   result = []
   for i in my_tickets: # List of lists
      for x in xs: # Picks
         if my_tickets[i] == xs[x]: #Not sure what my if statement is suppose to be
            result.append() # I don't know what goes in the parenthesis
    print (result)
    return (result)

lotto_matches([42,4,7,11,1,13], my_tickets)

我正在尝试编写一个函数,该函数接受一张票和一张平局的列表,并返回一个列表,说明每张票上的正确选择数。

它应该返回[1,2,3,1],但我的输出完全错误。

我该怎么做?

【问题讨论】:

    标签: python list function for-loop nested


    【解决方案1】:

    除非您的彩票抽奖有更多的球,否则创建集合的开销几乎不值得。

    def lotto_matches(xs, my_tickets):
        xs = set(xs) # optional - may not be any faster if xs is a small list
        return [sum(x in xs for x in t) for t in my_tickets]
    

    【讨论】:

      【解决方案2】:

      如果您需要一个衬里:

      output = [len(list(set(a_list) & set(draw))) for a_list in my_tickets]
      

      【讨论】:

      • 绘制 = [42,4,7,11,1,13]
      【解决方案3】:

      您可以像这样使用mapintersection

      def lotto_matches(pick, my_tickets):
          result = map(lambda x: len(set(x).intersection(pick)), my_tickets)
          return result
      
      print lotto_matches([42,4,7,11,1,13], my_tickets)
      

      pick 是您的比赛门票。

      如果您使用的是 Python 3,map 返回的迭代器可能不是您想要的,因此您需要

      return list(result)
      

      【讨论】:

      • 在你的函数中。这是整个函数的替代品。
      • 它从不使用我的第一个参数 xs。当我运行程序时,我什么也得不到。它应该看起来像这样吗? def lotto_matches(xs, my_tickets): pick = [7,21,22,26,34,43] map(lambda x: len(set(x).intersection(pick)), my_tickets) lotto_matches([42,4, 7,11,1,13],my_tickets)
      • @pakiboii Mate,我只是将其重命名为 pick,因为您确实应该为变量使用有意义的名称。
      【解决方案4】:
      1. 您没有在代码中正确累积结果。每次迭代都应该开始计数,并在最后追加结果。

      2. for i in my_tickets 迭代票证本身,而不是它们的索引。

      这里是固定代码:

      def lotto_matches(xs, my_tickets):
          results = []
          for ticket in my_tickets:
              result = 0
              for number in ticket:
                  for x in xs: 
                      if number == x: 
                          result += 1
              results.append(result)
          return result
      

      现在,通过扁平化你的循环,这可以变得更漂亮:

      1. 这个 if 语句

        if number == x: 
            result += 1
        

        可以利用True被解释为1的事实:

        result += (number==x)
        
      2. 这个

        result = 0
        for number in ticket:
            for x in xs: 
                result += (number==x)
        

        是使用嵌套循环计算的和,可以转换成这个

        result = sum(number==x for number in ticket for x in xs)
        

        这是检查ticket 中存在的所有x 值,可以使用in 进行简化(同样的,x in ticket 将在x 存在时返回True (1)在ticket):

        result = sum(x in ticket for x in xs)
        

        也就是说,如果xs 中的x 出现在ticket 中,我们希望在总和中加一。

      3. 现在,我们有了这个

        results = []
        for ticket in my_tickets:
            result = sum(x in ticket for x in xs)
            results.append(result)
        

        相当于

        results = [sum(x in ticket for x in xs) for ticket in my_tickets]
        
      4. 因此,您的整个代码可以是单行代码:

        def lotto_matches(xs, my_tickets):
            return [sum(x in ticket for x in xs) for ticket in my_tickets]
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-27
        • 2012-11-07
        • 1970-01-01
        相关资源
        最近更新 更多