我有以下清单:list_c = ['42.2529, -73.7910', '42.079846, -76.499364', '42.361824, -73.597979', '42.035959, -73.580146']我想转换成这个:list_c2 = [(42.2529, -73.7910),(42.079846, -76.499364),(42.361824, -73.597979),(42.035959, -73.580146)]我正在尝试的代码是:list_c2 = [(list_c[i]) for i in range(0, len(list_c))]print("list_c2 =", list_c)不幸的是,结果与 list_c
1 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
对不起,我最初误读了您的清单。要将其转换为浮点数对,您需要将split每个字符串放在其逗号上,然后将每个元素设为a float,然后将它们打包成一个元组:
list_c2 = [tuple(float(item) for item in s.split(',')) for s in list_c]
# [(42.2529, -73.791), (42.079846, -76.499364), (42.361824, -73.597979), (42.035959, -73.580146)]
添加回答
举报
0/150
提交
取消