特定输入下zip合并列表生成网格出现结果数量异常问题
网格生成函数zip合并后瓦片数量异常问题
我写了一个根据边界框生成网格的Python函数,最近发现特定输入条件下,用zip合并列表后得到的网格瓦片数量远低于预期的排列数。
最小可复现代例
import numpy as np def makegrid(xmin, ymin, xmax, ymax): xmin = int(np.floor(xmin / 1000) * 1000) ymin = int(np.floor(ymin / 1000) * 1000) xmax = int(np.ceil(xmax / 1000) * 1000) ymax = int(np.ceil(ymax / 1000) * 1000) left_list = [ left for bottom in range(ymin, ymax, 1000) for left in range(xmin, xmax, 1000) ] bottom_list = [ bottom for left in range(xmin, xmax, 1000) for bottom in range(ymin, ymax, 1000) ] right_list = [left + 1000 for left in left_list] top_list = [bottom + 1000 for bottom in bottom_list] # 编辑 - 添加打印语句显示输入维度 print(f"dims: {len(left_list)}, {len(bottom_list)}, {len(right_list)}, {len(top_list)}") zip_coords = zip(left_list,bottom_list,right_list,top_list) zcoords = list(zip_coords) print(f"expecting: {len(set(left_list)) * len(set(bottom_list))} tiles") print(f"produced: {len(set(zcoords))} tiles")
测试示例
示例一:运行正常
makegrid(452958.56 , 3684771.695, 551076.93 , 3846044.225)预期瓦片数:16300
实际生成:16300示例二:运行异常,实际生成量仅为预期的一小部分
makegrid(518249.995, 3630269.23 , 638616.78 , 3816623.475)预期瓦片数:22627
实际生成:2057示例三:对示例二参数微调后,恢复正常
makegrid(518249.995, 3640269.23 , 640616.78 , 3856623.475)预期瓦片数:26691
实际生成:26691示例四:另一种参数微调,仍不匹配预期
makegrid(519249.995, 3630169.23 , 638516.78 , 3812623.475)预期瓦片数:21960
实际生成:7320
已做排查
- 验证所有输入列表长度一致
- 尝试给
zip添加strict=True参数,仍未找到异常原因
请求帮忙定位问题。
内容的提问来源于stack exchange,提问作者Casey
相关产品推荐
相关产品推荐

