python列表中的多个数据类型
我有一个出生数据列表,每个记录有3列[出生日期、体重、身高],如下所示:python列表中的多个数据类型,python,list,datetime,types,floating-point,Python,List,Datetime,Types,Floating Point,我有一个出生数据列表,每个记录有3列[出生日期、体重、身高],如下所示: bd = [['10/03/2021 00:00','6.2', '33.3'],['12/04/2021 00:00','6.2', '33.3'], ['13/05/2021 00:00','6.2','33.3']] 我需要更改此文件的数据类型,因为它们都是列表中的字符串。我希望记录中的第一项是datetime,其余的是floats。我有: newdata = [i[0
bd = [['10/03/2021 00:00','6.2', '33.3'],['12/04/2021 00:00','6.2', '33.3'],
['13/05/2021 00:00','6.2','33.3']]
我需要更改此文件的数据类型,因为它们都是列表中的字符串。我希望记录中的第一项是datetime,其余的是floats。我有:
newdata = [i[0] for i in bd]
p= []
for x in bd:
xy = datetime.datetime.strptime(x,'%d/%m/%Y %H:%M') # this works to change the data type
p.append(xy) #it fails to update this to the new list
我得到一个属性错误:
AttributeError: 'str' object has no attribute 'append'
我想通过利用pythons文件IO操作来实现这一点。我还想将每个数据记录一起保存在主列表中的列表中。我只想更新数据类型。您的代码不完整,可能存在意外的变量覆盖率,您可以尝试直接使用列表
[[datetime.datetime.strptime(i[0],'%d/%m/%Y %H:%M'), float(i[1]), float(i[2])] for i in bd]