Home > Mobile >  How to loop multi-variable data in Python
How to loop multi-variable data in Python

Time:01-15

How to loop multi-variable data like this in python ?
I have latitude and longitude data and I want to pass all these value and run it for 5 times.

e.g.

round 1
lat = 13.29 , longitude = 100.34 city = 'ABC'

round 2
lat = 94.09834 ,longitude = 103.34 city = 'XYZ'

,... ,.. ,round 5

Very new to python world. Thank you for every kind comment and suggestion :)

CodePudding user response:

you can do something like this:

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)) 

Output :

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

CodePudding user response:

You should store the data in a proper data structure which will allow you to loop it, I would suggest storing it as a list of dictionaries. It would look something like this

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

The iteration over the data would look as following:

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

  •  Tags:  
  • Related