简体   繁体   中英

Python 3 - print order of for loop result

I have written a script in Python 3.6 which queries API data to return a postal address from a zip code.

The script is as follows:

import requests
import urllib.parse 

main_api = 'http://link.to.api_site/json?'
key = 'AbCdEfGhIjKlMnOpQrStUvWxYz0123456789'
address = '90210'

url = main_api + urllib.parse.urlencode({'address':address, 'key':key})

json_data = requests.get(url).json()

for result in json_data["results"][0]["address_components"]:
    print(result["long_name"])

The issue is have is the order the JSON data is given. When I run a query from the web browser the data is shown:

{
   "results" : [
     {
     "address_components" : [
        {
           "long_name" : "90210",
           "short_name" : "90210",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Beverly Hills",
           "short_name" : "Beverly Hills",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Los Angeles County",
           "short_name" : "Los Angeles County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "California",
           "short_name" : "CA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }

so when the For loop runs it returns the data in the following order:

90210
Beverly Hills
Los Angeles County
California
United States

What I would prefer is for the address to be printed out in a different order:

Beverly Hills
Los Angeles County
California
United States
90210

So question is, is there a way to change the order when the results of the For loop are printed out.

One simple way to do it is to store each information into variables and then you print each content in the correct order that you want:

import requests
import urllib.parse 

main_api = 'http://link.to.api_site/json?'
key = 'AbCdEfGhIjKlMnOpQrStUvWxYz0123456789'
address = '90210'

url = main_api + urllib.parse.urlencode({'address':address, 'key':key})

json_data = requests.get(url).json()

area_code = json_data["results"][0]["address_components"][0]["long_name"]
area = json_data["results"][0]["address_components"][1]["long_name"]
county = json_data["results"][0]["address_components"][2]["long_name"]
state = json_data["results"][0]["address_components"][3]["long_name"]
country = json_data["results"][0]["address_components"][4]["long_name"]

print(area)
print(county)
print(state)
print(country)
print(area_code)

You could also print the content of the list except for the first one and then "manually" the first one.

Maybe something like this? (I'm not sure this works, I haven't tested it).

for result in json_data["results"][0]["address_components"][1:]:
    print(result["long_name"])
print(json_data["results"][0]["address_components"][1]["long_name"])

You could try something like this;

order_to_print = ["locality", "administrative_area_level_2", "administrative_area_level_1", "country", "postal_code"]

for order in order_to_print:
    for result in json_data["results"][0]["address_components"]:
        if result["types"][0] == order:
            print(result["long_name"])

But I think it wouldn't be possible without defining the order like above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM