Py学习  »  Python

如何在Python中循环多变量数据

Paper.J • 3 年前 • 1422 次点击  

如何在python中循环这样的多变量数据?
我有纬度和经度数据,我想传递所有这些值并运行5次。

例如

第一轮
纬度=13.29,经度=100.34 城市='ABC'

第二轮
纬度=94.09834,经度=103.34 城市='XYZ'

,... ,.. ,第五轮

对python世界来说非常陌生。 感谢您的评论和建议:)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130090
 
1422 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Alik.Koldobsky
Reply   •   1 楼
Alik.Koldobsky    3 年前

你应该将数据存储在一个合适的数据结构中,这样你就可以循环它,我建议将它存储为一个字典列表。 看起来像这样

data = [{'city':'city1','lat':'lat1','alt':'alt1'},
{'city':'city2','lat':'lat2','alt':'alt2'}]

对数据的迭代如下所示:

for d in data:
    print(f" lat: {d['lat']}, alt: {d['alt']} , city: {d['city']}")

XxJames07-
Reply   •   2 楼
XxJames07-    3 年前

你可以这样做:

cities = ["Rome", "NYC"]
latitude = [41.2925, 40.730610]
longitude = [12.5736, 73.935242]
for (a, b, c) in zip(longitude, latitude, cities): #zips the lists together
     print ("long = %s, lat= %s, city:%s" % (a, b, c)) 

输出:

long = 12.5736, lat= 41.2925, city:Rome
long = 73.935242, lat= 40.73061, city:NYC