我有这个:
coordinates =
['54.9712958,-1.6094327',
'54.9697686,-1.6138101',
'54.969436,-1.6154838',
'54.9691035,-1.6177368',
'54.9685369,-1.619947',
'54.968463,-1.6229939']
我想要这样的东西(将100添加到列表中的每个值):
coordinates =
[(54.9712958,-1.6094327,100),
(54.9697686,-1.6138101,100),
(54.969436,-1.6154838,100),
(54.9691035,-1.6177368,100),
(54.9685369,-1.619947,100),
(54.968463,-1.6229939, 100)]
非常感谢您的帮助。
答案 0 :(得分:1)
您可以使用(嵌套的)列表理解([Python 3.Docs]: Data Structures - List Comprehensions)。
要注意的一件事是,在当前状态下,每个 coordinates 元素都是一个字符串,需要一些处理才能从中提取 float 。
>>> coordinates = ['54.9712958,-1.6094327', '54.9697686,-1.6138101', '54.969436,-1.6154838', '54.9691035,-1.6177368', '54.9685369,-1.619947', '54.968463,-1.6229939'] >>> >>> [(*(float(item1) for item1 in item0.split(",")), 100) for item0 in coordinates] [(54.9712958, -1.6094327, 100), (54.9697686, -1.6138101, 100), (54.969436, -1.6154838, 100), (54.9691035, -1.6177368, 100), (54.9685369, -1.619947, 100), (54.968463, -1.6229939, 100)]
答案 1 :(得分:1)
您需要
float
100
使用列表理解
coordinates = [(*list(map(float, item.split(","))), 100) for item in coordinates]
使用循环
result = []
for item in coordinates:
values = item.split(",")
result.append((float(values[0]), float(values[1]), 100))
答案 2 :(得分:0)
您需要遍历所有坐标,将它们用逗号分割(以获取x和y),然后使用这些值和100创建一个元组 然后,您可以将其附加到一些新列表中
coordinates = [
'54.9712958,-1.6094327',
'54.9697686,-1.6138101',
'54.969436,-1.6154838',
'54.9691035,-1.6177368',
'54.9685369,-1.619947',
'54.968463,-1.6229939'
]
new_coords = []
for coord_pair in coordinates:
coord_pair = coord_pair.split(",")
new_coords.append((float(coord_pair[0]), float(coord_pair[1]), 100))
new_coords
变量是:
[
(54.9712958, -1.6094327, 100),
(54.9697686, -1.6138101, 100),
(54.969436, -1.6154838, 100),
(54.9691035, -1.6177368, 100),
(54.9685369, -1.619947, 100),
(54.968463, -1.6229939, 100)
]
请注意,我不确定您是否希望将100设置为整型或浮点型,如果需要浮点型,只需将100
更改为float(100)
或{{1} }