简体   繁体   English

转换元组中的列表元素

[英]Convert elements of a list in tuples

I have the following list: 我有以下清单:

list_c = ['42.2529, -73.7910', '42.079846, -76.499364', '42.361824, -73.597979', '42.035959, -73.580146']

I'd like to convert to this: 我想转换为:

list_c2 =  [(42.2529, -73.7910),(42.079846, -76.499364),(42.361824, -73.597979),(42.035959, -73.580146)]

The code am trying is: 我正在尝试的代码是:

list_c2 = [(list_c[i]) for i in range(0, len(list_c))]
print("list_c2 =", list_c)

Unfortunately, the result is exactly the same as list_c 不幸的是,结果与list_c

I'm sorry, I misread you list initially. 抱歉,我最初看错了您的清单。 To convert this into pairs of floats, you'll need to split each string on its comma and then make each element a float , then pack them in a tuple: 要将其转换为成对的浮点数,您需要split每个字符串用逗号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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM