我有以下列表:
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
答案 0 :(得分:1)
对不起,我最初看错了您的清单。要将其转换为成对的浮点数,您需要以逗号分隔split
每个字符串,然后将每个元素都设为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)]