python - 如何在Python中连续连接子列表中的一对坐标?

标签 python python-2.7

我有以下坐标列表:

coords=[[(1,2),(3,4),(5,6)], [(7,8),(9,10)], [(11,12),(13,14),(15,16),(17,18)]]

如果可能的话,我想使用itertools python(不一定)将其转换为以下列表:

coords=[((1,2),(3,4)), ((3,4),(5,6)), ((7,8),(9,10)), ((11,12),(13,14)), ((13,14),(15,16)), ((15,16),(17,18))]

谢谢

我已尝试以下方法来迭代一个列表,但不迭代嵌套列表:

zip(coords[:-1],coords[1:]

最佳答案

您可以使用嵌套列表理解。通过将子列表与其自身压缩但偏移量为 1,将每个子列表中的相邻元组配对:

[p for t in coords for p in zip(t, t[1:])]

这将返回:

[((1, 2), (3, 4)), ((3, 4), (5, 6)), ((7, 8), (9, 10)), ((11, 12), (13, 14)), ((13, 14), (15, 16)), ((15, 16), (17, 18))]

关于python - 如何在Python中连续连接子列表中的一对坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57631100/

相关文章:

python - 如何从表单集中删除表单

python - 带有跳转主机和远程数据库的SSH隧道转发

python - Scrapy python 错误 - 请求 URL 中缺少方案

python - 几个测试的pytest参数化执行顺序

python - 使用 python 的 Electron 应用程序抛出 zmq.node 错误

python - 无法使用 python 3.5 安装 opencv 3.1,仅适用于 2.7

python - Python 中的异常问题

python - 如何传递 argparse 参数以充当 kwargs?

python - 检查一个单词是否在 Python 列表中

python - 最大()函数 : how to get the item being iterated and pass to key=func()