【问题标题】:How to loop multi-variable data in Python如何在 Python 中循环多变量数据
【发布时间】:2022-01-14 17:26:48
【问题描述】:

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

例如

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

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

,... ,.. ,第5轮

对 python 世界非常陌生。 感谢您的每一个好意见和建议:)

【问题讨论】:

  • 此数据是否已存在于您的程序中? “传递所有这些值并运行 5 次”是什么意思?您可以制作一个包含 5 个“纬度/经度/城市”对象的列表的程序,然后遍历这些对象并对数据进行处理。但如果没有更多细节,就不清楚你想要实现什么
  • 如果您的五轮在字典列表中,那么这可能会有所帮助:stackoverflow.com/questions/35864007/…

标签: python loops for-loop


【解决方案1】:

你可以这样做:

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

【讨论】:

    【解决方案2】:

    您应该将数据存储在允许您循环它的适当数据结构中,我建议将其存储为字典列表。 它看起来像这样

    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']}")
    
    

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 1970-01-01
      • 2017-01-20
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      相关资源
      最近更新 更多