对象类型的转换
Conversion of types of objects
如何在 python 上编写一个函数,在输入中接受两个对象并在输出中给出两个对象都可以呈现的最小类型?
示例:我们有两个对象:1
和 5.25
。我们不能将它们都转换为 int
,因为那样我们将丢失有关 5.25
的信息将转换为 5
。我们可以将它们都转换为 float
:1.0
和 5.25
,这是正确答案。当然我们可以说我们可以将它们都转换为str
:"1"
、"5.25"
,但是在我们的解释中我们假设int < float < tuple < list < str
(当然我们不能比较类型的对象,但我们假设要得到答案)然后 float
是两个对象都可以转换为的最小可用类型。
我试过类似的方法:
def type_convertion(first, second):
data_types = [int, float, tuple, list, str]
times = {}
for _type in data_types:
times[_type] = 0
try:
if isinstance(_type(first), _type):
times[_type] += 1
except TypeError:
del times[_type]
continue
try:
if isinstance(_type(second), _type):
times[_type] += 1
except TypeError:
del times[_type]
continue
return times.keys()
但是当我比较 int
和 float
时,答案是 int
但应该是 float
并且我不知道如何解决它。
如果我很理解你的问题,你想获得可以匹配两个变量的 minimum/best 类型。
我已经将你的排名顺序更新如下int < float < str < tuple < list
,但如果你愿意,你仍然可以保留你的排名。所以这是一个函数,它将几个变量作为参数,returns 匹配这些变量的最小类型的名称:
def type_convertion(*variables):
data_types = {'int': 0, 'float': 1, 'str': 2, 'tuple': 3, 'list': 4} # types and their rankings
minimum_type = [0, ''] # this list will contain the ranking and the name of the minimum type
for variable in variables:
variable_type = type(variable).__name__ # get the name of the variable type
if variable_type not in data_types:
# if the type is not reconized we can always convert it in str
return 'str'
if data_types[variable_type] > minimum_type[0]:
# if the variable type is of higher rank from the previous one then change the minimum variable
minimum_type = [data_types[variable_type], variable_type]
return minimum_type[1]
*variables
允许您为函数提供任意多的参数,因此您不限于 2 个变量。
要获得最小类型,请像这样调用函数:
>>> type_convertion('a', 10, 0.5)
'str'
如何在 python 上编写一个函数,在输入中接受两个对象并在输出中给出两个对象都可以呈现的最小类型?
示例:我们有两个对象:1
和 5.25
。我们不能将它们都转换为 int
,因为那样我们将丢失有关 5.25
的信息将转换为 5
。我们可以将它们都转换为 float
:1.0
和 5.25
,这是正确答案。当然我们可以说我们可以将它们都转换为str
:"1"
、"5.25"
,但是在我们的解释中我们假设int < float < tuple < list < str
(当然我们不能比较类型的对象,但我们假设要得到答案)然后 float
是两个对象都可以转换为的最小可用类型。
我试过类似的方法:
def type_convertion(first, second):
data_types = [int, float, tuple, list, str]
times = {}
for _type in data_types:
times[_type] = 0
try:
if isinstance(_type(first), _type):
times[_type] += 1
except TypeError:
del times[_type]
continue
try:
if isinstance(_type(second), _type):
times[_type] += 1
except TypeError:
del times[_type]
continue
return times.keys()
但是当我比较 int
和 float
时,答案是 int
但应该是 float
并且我不知道如何解决它。
如果我很理解你的问题,你想获得可以匹配两个变量的 minimum/best 类型。
我已经将你的排名顺序更新如下int < float < str < tuple < list
,但如果你愿意,你仍然可以保留你的排名。所以这是一个函数,它将几个变量作为参数,returns 匹配这些变量的最小类型的名称:
def type_convertion(*variables):
data_types = {'int': 0, 'float': 1, 'str': 2, 'tuple': 3, 'list': 4} # types and their rankings
minimum_type = [0, ''] # this list will contain the ranking and the name of the minimum type
for variable in variables:
variable_type = type(variable).__name__ # get the name of the variable type
if variable_type not in data_types:
# if the type is not reconized we can always convert it in str
return 'str'
if data_types[variable_type] > minimum_type[0]:
# if the variable type is of higher rank from the previous one then change the minimum variable
minimum_type = [data_types[variable_type], variable_type]
return minimum_type[1]
*variables
允许您为函数提供任意多的参数,因此您不限于 2 个变量。
要获得最小类型,请像这样调用函数:
>>> type_convertion('a', 10, 0.5)
'str'