0

首先很抱歉这个简单的问题。我有一个清单(只是一个例子)

points = [[663963.7405329756, 6178165.692240637],
 [664101.4213951868, 6177971.251818423],
 [664099.7474887948, 6177963.323432223],
 [664041.432877932, 6177903.295650704],
 [664031.8017317944, 6177895.797176996],
 [663963.7405329756, 6178165.692240637]]

我需要将其转换为以下形式

points = [(663963.7405329756, 6178165.692240637),
 (664101.4213951868, 6177971.251818423),
 (664099.7474887948, 6177963.323432223),
 (664041.432877932, 6177903.295650704),
 (664031.8017317944, 6177895.797176996),
 (663963.7405329756, 6178165.692240637)]

为了使用shapely 模块Polygon创建对象。我写了几个循环,但真的不优雅且耗时。您知道将第一个列表转换为第二个列表的最佳方法吗?

谢谢

4

4 回答 4

5
converted = map(tuple, points) # Python 2
converted = list(map(tuple, points)) # or BlackBear's answer for Python 3
converted = [tuple(x) for x in points] # another variation of the same
于 2013-01-08T21:12:15.650 回答
2
converted = [(a,b) for a,b in points]
于 2013-01-08T21:11:02.747 回答
2
converted = [tuple(l) for l in points]

与@BlackBear 给出的解决方案相比,这适用于任意大小的子列表。

于 2013-01-08T21:16:28.827 回答
1
points = [tuple(x) for x in points]
于 2013-01-08T21:14:30.633 回答