Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 减少元组列表_Python_Tuples_Reduce - Fatal编程技术网

Python 减少元组列表

Python 减少元组列表,python,tuples,reduce,Python,Tuples,Reduce,作为学习python之旅的一部分,我正在实现Bulls and Cows。 我有一个使用列表理解的工作实现,但我认为使用生成器和reduce()-ing最终结果可能是一个很好的解决方案 所以我有我的发电机: def bullsandcows(given, number): for i in range(given.__len__()): if given[i] == number[i]: yield (given[i], None)

作为学习python之旅的一部分,我正在实现Bulls and Cows。
我有一个使用列表理解的工作实现,但我认为使用生成器和
reduce()
-ing最终结果可能是一个很好的解决方案

所以我有我的发电机:

def bullsandcows(given, number):
    for i in range(given.__len__()):
        if given[i] == number[i]:
            yield (given[i], None)
        elif given[i] in number:
            yield (None, given[i])
以及我的实施:

(bulls, cows) = reduce(\
    lambda (bull, cow), (b, c): \
        (bull + 1, cow + 1), bullsandcows(given, number), (0, 0))
其中,
given
是用户输入,
number
是随机生成的供用户猜测的数字

正如您所看到的,这并不是一个有效的实现,它只返回
yield
ed元组的计数

我需要的是替换
(牛+1,牛+1)
,我不知道如何构造它

  • number
    是一个随机生成的数字,例如:
    1234
  • 给定的
    由用户输入,例如:
    8241
  • bullsandcows(给定,数字)
    的结果将是:
    [('2',无),(无,'4'),(无,'1']
  • reduce
    的结果应该是:
    (1,2)
    ,这是第一个元素的所有非
    None
    值的计数和第二个元素的所有非
    None
    值的计数

    • 如果我正确理解了这个过程,您需要计算哪些
      bull
      s不是
      None
      ,以及有多少
      cow
      s不是
      None

      reduce(lambda (bcount, ccount), (b, c): (bcount + (b is not None), ccount + (c is not None)),
             bullsandcows(given, number), (0, 0))
      
      仅当
      bull
      cow
      值不是
      None
      时,此测试才会增加计数器。该测试生成一个布尔值,它是
      int
      的一个子类,具有
      False==0
      True==1;将一个整数和一个布尔值相加,得到另一个整数

      由于您正在向其输入非空字符串,因此可以将其简化为:

      reduce(lambda (bcount, ccount), (b, c): (bcount + bool(b), ccount + bool(c)),
             bullsandcows(given, number), (0, 0))
      
      我将
      bullsandcows()
      重写为:

      def bullsandcows(given, number):
          given, number = map(str, (given, number))
          for g, n in zip(given, number):
              if g == n:
                  yield (g, None)
              elif g in number:
                  yield (None, g)
      
      e、 g.使用
      zip()
      给定的
      数字
      的数字配对

      演示:

      请注意,函数参数中的解包已从Python3中删除,并且内置的
      reduce()
      已委托给库函数;您的代码显然只是Python2

      要使其在Python 3中工作,需要导入并调整lambda,使其不使用解包:

      from functools import reduce
      
      reduce(lambda counts, bc: (counts[0] + bool(bc[0]), counts[1] + bool(bc[1])),
             bullsandcows(given, number), (0, 0))
      

      len(给定)
      更清晰时,不要使用
      ,当可以使用
      zip()时,不要使用
      范围()
      相反。你是对的,修复了这个问题。它似乎没有忽略
      牛群和奶牛的输出。结果总是一个包含来自生成器的结果数的两元素元组。你忽略了lambda中的
      (b,c)
      元组。这是正确的,这也是我的问题:)我不清楚输入是什么,以及您期望的输出是什么。
      elif g in str(number):
      @mtadd:was get to that.:-)谢谢这完全解决了我的问题。我希望我能通过解释如何以及为什么我可以改进我的代码,为你投下另一张赞成票
      from functools import reduce
      
      reduce(lambda counts, bc: (counts[0] + bool(bc[0]), counts[1] + bool(bc[1])),
             bullsandcows(given, number), (0, 0))