简体   繁体   中英

Python script won't stop running

I'm creating a dictionary for my map to aggregate all counties/states by latitude and longitude for a sales territory map however it won't stop running though I tried it just last night and it worked perfectly.

I've restarted several times.

Territory1 = ["Austin","Bay Area - SF","Bay Area - SV","Bold North","Chicago 1","Chicago 2","Dallas 1","Dallas 2","Desert","Georgia","Great Lakes","LA","LA 2","Maryland Metro","Metro Central","Metro East","Mid South","Mid-Atlantic","Missouri Valley","New England 1","New England 2","NY Metro","OC/LA","Ohio Valley","Peninsula","Philly East","Philly West","PNW 1","PNW 2","PNW 3","Rockies","So Cal","South","SoVA"]

for territory in Territory1:

    territory_data = dict(
                        lat = df.loc[df["Territory1"]==territory,"Lat"],
                        lon = df.loc[df["Territory1"]==territory,"Lon"],
                        name = territory,
                        marker = dict(size = 8, opacity = 0.5),
                        type = 'scattermapbox')
    Territory1.append(territory_data)

What I want is an aggregated list that will show up in the map that users can click to see each territory individually (the territories are as listed in Territory1 list).

It will not stop because:

Territory1.append(territory_data)

appends new elements in the Territory1 list and then

for territory in Territory1:

does not reach an end.

Probably you need: results_list.append(territory_data)

As @serafeim said, you're iterating over a list while you're adding to it.

   Territory1 = ["Austin","Bay Area - SF","Bay Area - SV","Bold North","Chicago 1","Chicago 2","Dallas 1","Dallas 2","Desert","Georgia","Great Lakes","LA","LA 2","Maryland Metro","Metro Central","Metro East","Mid South","Mid-Atlantic","Missouri Valley","New England 1","New England 2","NY Metro","OC/LA","Ohio Valley","Peninsula","Philly East","Philly West","PNW 1","PNW 2","PNW 3","Rockies","So Cal","South","SoVA"]
   for territory in Territory1:

        territory_data = dict(
                          lat = df.loc[df["Territory1"]==territory,"Lat"],
                          lon = df.loc[df["Territory1"]==territory,"Lon"],
                          name = territory,
                          marker = dict(size = 8, opacity = 0.5),
                          type = 'scattermapbox')
        Territory1.append(territory_data)

I believe what you want to do is:

territory_dicts = {}
for territory in Territory1:
    temp_dict = dict(lat: df.loc[df["Territory1"]==territory,"Lat"],
                     lon: df.loc[df["Territory1"]==territory,"Lon"],
                     name: territory,
                     marker: dict(size = 8, opacity = 0.5),
                     type: 'scattermapbox')
    territory_dicts[territory] = temp_dict

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