简体   繁体   English

python:优雅且节省代码的方式来转换列表

[英]python: elegant and code saving way to convert a list

First of all sorry for the easy question. 首先,对这个简单的问题感到抱歉。 I have a list (just an example) 我有一个清单(只是一个例子)

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]]

I need to convert it to the following form 我需要将其转换为以下形式

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)]

in order to create a Polygon object using shapely module . 为了使用shapely模块创建一个Polygon对象。 I wrote several loops but really not elegant and time consuming. 我编写了几个循环,但实际上并不优雅且耗时。 Do you know the best way to convert the first list into the second? 您知道将第一个列表转换为第二个列表的最佳方法吗?

Thanks 谢谢

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
converted = [(a,b) for a,b in points]
converted = [tuple(l) for l in points]

与@BlackBear提供的解决方案相比,此方法适用于任意大小的子列表。

points = [tuple(x) for x in points]

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

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