Skip to content

跟老齐学Python 这个例子怎么运行不对? #127

Description

@pendave

35ed d60 l160vp9 3t 47e

g9 of5_ 61vmf a q5 hrkh

jh655ie j14 njoz 3lvlwy

reduce(lambda sum,(x,y): sum+x*y,zip(a,b),0)
我直接复制出来

Activity

  1. qiwsir commented on Dec 23, 2017

    @qiwsir
    Owner
  2. jintianming commented on Jan 8, 2024

    @jintianming
  3. fish1968 commented on Jan 8, 2024

    @fish1968

    https://stackoverflow.com/questions/11328312/python-lambda-does-not-accept-tuple-argument 参考这里,使用 tuple 作为 lambda 参数的特性在 python3 中被取消了,可以改为

    reduce(add,map(lambda x: x[0]*x[1], zip(a,b)))
    reduce(lambda x,y: x+y, map(lambda x,y: x*y, a, b))
    reduce(lambda x,y: x+y[0]*y[1], zip(a,b),0) # 0 must be provided here, otherwise the first tuple of zip(a,b) would be the initial value, and error happens due to tuple type added with int type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions