python datatype函数_在datatype上使用python字典“switch”

我正在尝试用python编写一个“switch”字典。我希望能够从文本文件中读入数据,并根据其数据类型执行不同的操作。例如,如果我读入一个字符串,我想把它与另一个字符串进行比较。或者如果我读一个浮点数,我想用它做一些运算。这是机器学习程序的数据清理操作。在

我也许可以用If…Else语句来实现,但是由于我可以想象得到每种数据类型都有一些内容,所以我宁愿做得更干净。在

我正在使用以下代码:varX = 2.0

switchDict = {"bool": "boolean", "int": "integer","float": "floatType",

"str": "string"}

switchDict[str(type(varX))]()

def boolean():

print("You have a boolean" )

def integer():

print("You have an integer")

def floatType():

print("You have a float")

def string():

print("You have a string”)

它返回:

^{pr2}$

如果我将switchDict行更改为:switchDict = {bool: "boolean", int: "integer", float: "floatType", str: "string"}

switchDict[type(varX)]()

它返回:Traceback (most recent call last):

File "/Gower71/Switch.py", line 5, in

switchDict[type(varX)]()

TypeError: 'str' object is not callable

有没有办法打开这样的字体??在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值