简体   繁体   中英

Lookup element in python list using each value from another list

I've tried for in loops, while true loops, next, etc. Either I get information repeated over and over, or the loop does not start over once it finds an element. I've also searched on SO and google for a solution but without success.

What I am trying to do is this:

for feature in parJson:
    ids = feature['properties']['id']
    event = feature['properties']['event']
    headline = feature['properties']['headline']
    expires = feature['properties']['expires']
    areaDesc = feature['properties']['areaDesc']
    geoCodes = feature['properties']['geocode']['UGC']

    states = alerts.getZones(geoCodes)

def getZones(self, getZones):
    zones = json.loads(open("resources/zones.json").read())
    parsedZones = zones['features']
    #TODO figure out how to compare this list!
    states = []
    for ugcCode in getZones:
        ugcCode = str(ugcCode)
        for element in parsedZones:
            ugc = element['properties']['id']
            state = element['properties']['state']
            if ugc == ugcCode:
                states.append(state)
                break

    print(states)

getZones is a list of UGC geocode zones. parsedZones is a json list of watches warnings advisories from api.weather.gov/alerts/active.

And the data:

the parsedZones: https://api.weather.gov/zones (downloaded to disk because it is so large)

the parJson: https://api.weather.gov/alerts/active

From the list of active alerts, I get the ugc codes the warning is valid for. The api does not give the state, only those codes. So I need to decode the codes into state names. I already have a dictionary with state names/abbreviations. What I need is a python set of the unique states based off the UGC codes listed in the warning. Once I have the states, I can lookup each state name using its abbreviation, then display the state. So it would go like this:

Let's say a warning is valid for Texas and Oklahoma. And the codes provided are OKC001, and TXC001. I can't just use startswith to get the first two letters, because some areas don't start with the state abbreviation. Therefore, I must use a zones list provided by the api, which I downloaded to my server. I need to lookup each code provided one at a time and retrieve the two letter abbreviation. I can work with it after I get that. But I can't seem to get it to work. The goal is to add each state abbreviation to a set to get unique values.

The end result of what I have done so far is this:

[] None
Severe Thunderstorm Watch
Windsor; Orange; Rutland
[] None [] None
Flood Warning
DeKalb; Marshall
['IN', 'IN', 'IN'] None
Special Weather Statement
Miami; Wabash; Huntington
['PA', 'PA', 'PA'] None
Special Weather Statement
Wyoming; Lackawanna; Luzerne
[] None
Flood Advisory
Sequoyah
['NY', 'NY', 'NY', 'NY', 'NY', 'NY', 'NY'] None
Special Weather Statement
Onondaga; Madison; Northern Oneida; Southern Oneida; Cortland; Otsego; Chenango

Your problem is that the loop is resulting in all of the variables being overwritten for each element you're looping through.

states = []
for feature in parJson:
    ...
    states.append(alerts.getZones(geoCodes))
print(states)

This will result in a list of the states in question. You may want to store this info all in a nested dictionary with each state being a key and the various items you want to know being lists that you append if you have additional information instead of overwriting...

I found the problem. I had uploaded a new file with the zones but it didn't actually overwrite the old file. Used wget to retrieve the file and it solved the problem. Thank you to @mauve for their suggestion. It did help with my issue once I got the correct data.

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