具有包含数字字符串的列表,如下所示:
numbers = ['1.2', '5.6', '1'];
想要将前两个列表元素转换为float,将最后一个元素转换为int:
numbers = [1.2, 5.6, 1];
有没有一种班轮/优雅的方式?
当前我正在处理:
numbers = [ float(x) for x in numbers ]
numbers[-1] = int(numbers[-1])
或
functions = [float, float, int]
results = [func(arg) for func, arg in zip(functions, numbers)]