【问题标题】:TypeError: FunctionSuppliedToMap takes 0 positional arguments but 1 was givenTypeError:FunctionSuppiedToMap 采用 0 个位置参数,但给出了 1 个
【发布时间】:2019-09-22 05:30:18
【问题描述】:
def MapperGender():
    if gender=='Male':
        return 0 
    else:
        return 1

data['GenderMapped']=list(map(MapperGender,data['Sex']))

出现错误:

TypeError 
Traceback (most recent call last)
<ipython-input-35-99063d9bb2fe> in <module>
----> 1 data['GenderMapped']=list(map(MapperGender,data['Sex']))

TypeError: MapperGender() takes 0 positional arguments but 1 was given

【问题讨论】:

  • 查看标题,澄清..它不是“任何”功能,甚至不是“地图”功能..它是以特定方式使用的特定功能,如错误消息中所述。我只是为函数名称添加了一个更通用的占位符,说明用法。

标签: python dictionary


【解决方案1】:

你的函数需要一个参数

def MapperGender(gender):
    if gender=='Male':
        return 0 
    else:
        return 1

data={"Sex":["Male","Female"]}
data['GenderMapped']=list(map(MapperGender,data['Sex']))
print(data) 
# Output : {'GenderMapped': [0, 1], 'Sex': ['Male', 'Female']}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多