python - 在元组中循环直到达到某个索引

标签 python

我有这个函数可以创建格式化的 WKT 字符串

def _dump_point(obj, fmt, dim=2):
    coords = obj['coordinates']
    pt = 'POINT (%s)' % ' '.join(fmt % c for c in coords)
    return pt

coords 是一个浮点元组,可以有 2 或 3 个元素,如 (0.3455 , 4.234)(24.324 , 3.123 , 4.545)

我想使用 dim 参数在坐标中循环直到某个索引。如果 dim 是 2,我想加入元组的前 2 项,忽略第三项。

最佳答案

尝试只采用坐标[:dim]

pt = 'POINT (%s)' % ' '.join(fmt % c for c in coords[:dim])

关于python - 在元组中循环直到达到某个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163425/

相关文章:

函数内部的 Python Try-Except

python - 在哪里可以将保存确认页面连接到 Django 管理员? (类似删除确认)

python - 如何在Python中解析带有编码声明的XML文件?

python - 读取股票数据时出错 : 'DatetimeProperties' object has no attribute 'weekday_name' and 'NoneType' object has no attribute 'to_csv'

python - 为什么 "\p{L}"在此正则表达式中不起作用?

python - pep 232 也适用于实例方法吗?

python:通过集合的子集为循环创建迭代器

python - 添加 "search"同时保持分页和排序?

python - itertools.product 比嵌套 for 循环慢

python - 转换自然范围外的角度(对于该范围内的 theta : 0 to 180 degrees) to an equivalent angle,