【发布时间】:2017-10-30 05:07:40
【问题描述】:
如何将列表中的数据转换为正确的类型,即 int 如果是整数,float 如果不是整数,bool 如果是真或假?
def clean_data(data: List[list]) -> None:
"""Convert each string in data to an int if and only if it represents a
whole number, a float if and only if it represents a number that is not a
whole number, True if and only if it is 'True', False if and only if it is
'False', and None if and only if it is either 'null' or the empty string.
>>> d = [['abc', '123', '45.6', 'True', 'False']]
>>> clean_data(d)
>>> d
[['abc', 123, 45.6, True, False]]
【问题讨论】:
标签: string python-3.x list loops