如何删除 double/nested for 循环? python 中的字符串到浮点数的转换
How to to remove a double/nested for-loop? String to float transformation in python
我有以下地理区域的多边形,我通过 CAP/XML 格式的请求从 API
获取
原始数据是这样的:
<polygon>22.3243,113.8659 22.3333,113.8691 22.4288,113.8691 22.4316,113.8742 22.4724,113.9478 22.5101,113.9951 22.5099,113.9985 22.508,114.0017 22.5046,114.0051 22.5018,114.0085 22.5007,114.0112 22.5007,114.0125 22.502,114.0166 22.5038,114.0204 22.5066,114.0245 22.5067,114.0281 22.5057,114.0371 22.5051,114.0409 22.5041,114.0453 22.5025,114.0494 22.5023,114.0511 22.5035,114.0549 22.5047,114.0564 22.5059,114.057 22.5104,114.0576 22.512,114.0584 22.5144,114.0608 22.5163,114.0637 22.517,114.0657 22.5172,114.0683 22.5181,114.0717 22.5173,114.0739</polygon>
我将请求的项目存储在字典中,然后根据我正在使用的模式将它们转换为适合摄取到 Elasticsearch 的 GeoJSON 列表对象。为了方便阅读,我在这里删除了不相关的代码。
# fetches and store data in a dictionary
r = requests.get("https://alerts.weather.gov/cap/ny.php?x=0")
xpars = xmltodict.parse(r.text)
json_entry = json.dumps(xpars['feed']['entry'])
dict_entry = json.loads(json_entry)
# transform items if necessary
for entry in dict_entry:
if entry['cap:polygon']:
polygon = entry['cap:polygon']
polygon = polygon.split(" ")
coordinates = []
# take the split list items swap their positions and enclose them in their own arrays
for p in polygon:
p = p.split(",")
p[0], p[1] = float(p[1]), float(p[0]) # swap lon/lat
coordinates += [p]
# more code adding fields to new dict object, not relevant to the question
p in polygon
循环的输出如下所示:
[ [113.8659, 22.3243], [113.8691, 22.3333], [113.8691, 22.4288], [113.8742, 22.4316], [113.9478, 22.4724], [113.9951, 22.5101], [113.9985, 22.5099], [114.0017, 22.508], [114.0051, 22.5046], [114.0085, 22.5018], [114.0112, 22.5007], [114.0125, 22.5007], [114.0166, 22.502], [114.0204, 22.5038], [114.0245, 22.5066], [114.0281, 22.5067], [114.0371, 22.5057], [114.0409, 22.5051], [114.0453, 22.5041], [114.0494, 22.5025], [114.0511, 22.5023], [114.0549, 22.5035], [114.0564, 22.5047], [114.057, 22.5059], [114.0576, 22.5104], [114.0584, 22.512], [114.0608, 22.5144], [114.0637, 22.5163], [114.0657, 22.517], [114.0683, 22.5172], [114.0717, 22.5181], [114.0739, 22.5173] ]
有没有比 O(N^2) 更好的方法来做到这一点?感谢您花时间阅读。
O(KxNxM)
这个过程涉及三个明显的循环。它们是:
- 检查每个条目 (K)
- 将有效条目拆分为点 (MxN) 并遍历这些点 (N)
- 将这些点拆分为各自的坐标 (M)
多边形字符串中的字母数量为 ~MxN,因为有 N 个点,每个点大约有 M 个字母长,因此它遍历 MxN 个字符。
现在我们知道了所有这些,让我们找出每个发生的位置。
ENTRIES (K):
IF:
SPLIT (MxN)
POINTS (N):
COORDS(M)
所以,我们最终可以得出结论,这是O(K(MxN + MxN))
,也就是O(KxNxM)
。
我有以下地理区域的多边形,我通过 CAP/XML 格式的请求从 API
获取原始数据是这样的:
<polygon>22.3243,113.8659 22.3333,113.8691 22.4288,113.8691 22.4316,113.8742 22.4724,113.9478 22.5101,113.9951 22.5099,113.9985 22.508,114.0017 22.5046,114.0051 22.5018,114.0085 22.5007,114.0112 22.5007,114.0125 22.502,114.0166 22.5038,114.0204 22.5066,114.0245 22.5067,114.0281 22.5057,114.0371 22.5051,114.0409 22.5041,114.0453 22.5025,114.0494 22.5023,114.0511 22.5035,114.0549 22.5047,114.0564 22.5059,114.057 22.5104,114.0576 22.512,114.0584 22.5144,114.0608 22.5163,114.0637 22.517,114.0657 22.5172,114.0683 22.5181,114.0717 22.5173,114.0739</polygon>
我将请求的项目存储在字典中,然后根据我正在使用的模式将它们转换为适合摄取到 Elasticsearch 的 GeoJSON 列表对象。为了方便阅读,我在这里删除了不相关的代码。
# fetches and store data in a dictionary
r = requests.get("https://alerts.weather.gov/cap/ny.php?x=0")
xpars = xmltodict.parse(r.text)
json_entry = json.dumps(xpars['feed']['entry'])
dict_entry = json.loads(json_entry)
# transform items if necessary
for entry in dict_entry:
if entry['cap:polygon']:
polygon = entry['cap:polygon']
polygon = polygon.split(" ")
coordinates = []
# take the split list items swap their positions and enclose them in their own arrays
for p in polygon:
p = p.split(",")
p[0], p[1] = float(p[1]), float(p[0]) # swap lon/lat
coordinates += [p]
# more code adding fields to new dict object, not relevant to the question
p in polygon
循环的输出如下所示:
[ [113.8659, 22.3243], [113.8691, 22.3333], [113.8691, 22.4288], [113.8742, 22.4316], [113.9478, 22.4724], [113.9951, 22.5101], [113.9985, 22.5099], [114.0017, 22.508], [114.0051, 22.5046], [114.0085, 22.5018], [114.0112, 22.5007], [114.0125, 22.5007], [114.0166, 22.502], [114.0204, 22.5038], [114.0245, 22.5066], [114.0281, 22.5067], [114.0371, 22.5057], [114.0409, 22.5051], [114.0453, 22.5041], [114.0494, 22.5025], [114.0511, 22.5023], [114.0549, 22.5035], [114.0564, 22.5047], [114.057, 22.5059], [114.0576, 22.5104], [114.0584, 22.512], [114.0608, 22.5144], [114.0637, 22.5163], [114.0657, 22.517], [114.0683, 22.5172], [114.0717, 22.5181], [114.0739, 22.5173] ]
有没有比 O(N^2) 更好的方法来做到这一点?感谢您花时间阅读。
O(KxNxM)
这个过程涉及三个明显的循环。它们是:
- 检查每个条目 (K)
- 将有效条目拆分为点 (MxN) 并遍历这些点 (N)
- 将这些点拆分为各自的坐标 (M)
多边形字符串中的字母数量为 ~MxN,因为有 N 个点,每个点大约有 M 个字母长,因此它遍历 MxN 个字符。
现在我们知道了所有这些,让我们找出每个发生的位置。
ENTRIES (K):
IF:
SPLIT (MxN)
POINTS (N):
COORDS(M)
所以,我们最终可以得出结论,这是O(K(MxN + MxN))
,也就是O(KxNxM)
。