Привет, я пытаюсь создать словарь, который содержит список лат и лонг для каждого дня.

Пожалуйста, посмотрите образец данных:

  date            latitude          longitude
1/01/2012          135.5             149.5
1/01/2012          138.8             151.5
3/02/2012          134.7             143.8

В идеале дата должна быть ключевой в словаре, а лат и длинная - списком кортежей.

  {'1/01/12' : [(135.5 , 149.5),(138.8 ,151.5)] ,'3/02/2012': [(134.7, 143.8)]}         

Пожалуйста, смотрите мой код ниже, проблема в том, что он добавляет только один из местоположений.

with open("file", 'r') as f:
    reader = csv.reader(f, delimiter = ',')
    headers = next(reader)
    accidents_daily = []
    for line in f:
        line = line.strip().split(',')
        date = line[1]
        location = (line[4],line[5])##represents my lat and long

        locations_daily[crash_date] = (location)

Выборка необработанных данных:

CRASH_ID,CRASH_DATE,CRASH_TIME,SUBURB_LOCATION,LONGITUDE,LATITUDE,INTERSECTION,MIDBLOCK,CRASH_DIRECTION,CRASH_SEVERITY,LIGHTING_CONDITION,ROAD_CONDITION,WEATHER_CONDITION,Location
1143355,31/08/2016,15:20,WARAMANGA,149.0646898,-35.34840034,NO,YES,East / South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.348400344091395, 149.06468975103948)"
1191616,23/08/2018,18:15,PHILLIP,149.0848095,-35.34886707,NO,YES,West bound,Property Damage Only,Dark - good street lighting,Good dry surface,Cloudy or Overcast,"(-35.34886707244341, 149.08480946267707)"
1120068,9/09/2015,11:15,TURNER,149.1298864,-35.27342246,YES,NO,North bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.273422461411066, 149.12988641734273)"
1080387,28/01/2014,8:30,TURNER,149.1306254,-35.26040749,YES,NO,East / West bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.26040749341896, 149.13062535940057)"
1076245,19/11/2013,10:00,CAMPBELL,149.1473753,-35.282155,YES,NO,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.282155004486896, 149.14737534275548)"
1109287,15/04/2015,17:30,BELCONNEN,149.0710302,-35.23817334,NO,YES,North bound,Property Damage Only,Daylight,Wet surface,Cloudy or Overcast,"(-35.238173337249656, 149.07103019391346)"
1152056,27/01/2017,13:42,YARRALUMLA,149.1070649,-35.30479792,YES,NO,East / South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.30479791661196, 149.10706487573066)"
1030906,28/02/2012,6:30,BRUCE,149.0887509,-35.25094292,YES,NO,South bound,Property Damage Only,Dark - good street lighting,Wet surface,Light rain,"(-35.250942917963485, 149.0887508999686)"
1119850,7/09/2015,12:10,RURAL - TUGGERANONG,149.1365865,-35.39028997,YES,NO,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.39028997205093, 149.1365865448212)"
1061849,18/05/2013,12:30,KINGSTON,149.1441428,-35.31463096,NO,YES,North bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.314630964653354, 149.14414282152543)"
1059943,2/04/2013,11:00,CAMPBELL,149.1551892,-35.28166816,NO,YES,West bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.281668158228086, 149.15518920244364)"
1071721,15/09/2013,13:20,WESTON,149.0522999,-35.34143873,NO,YES,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.341438728406914, 149.05229989973964)"
1089318,20/06/2014,7:50,STIRLING,149.0568903,-35.34779082,YES,NO,North / East bound,Injury,Daylight,Good dry surface,Fine,"(-35.347790821523894, 149.05689028104578)"
1164570,20/07/2017,13:30,BRADDON,149.1433425,-35.27602195,YES,NO,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.276021954576805, 149.14334246747734)"
1057813,15/03/2013,17:00,CITY,149.1306045,-35.27593626,NO,YES,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.275936262233344, 149.13060450922876)"
1026206,3/01/2012,7:30,RURAL - CANBERRA CENTRAL,149.0881433,-35.2589105,NO,YES,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.258910500452345, 149.08814331874453)"
1083445,15/03/2014,16:14,RURAL - TUGGERANONG,149.1170637,-35.38322518,YES,NO,West bound,Property Damage Only,Daylight,Good dry surface,Cloudy or Overcast,"(-35.383225176666116, 149.1170637438708)"
1162844,21/06/2017,10:05,GUNGAHLIN,149.1208645,-35.18561166,NO,YES,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.185611661788485, 149.12086450236487)"
1107684,20/03/2015,16:30,YARRALUMLA,149.1067534,-35.31091769,YES,NO,East / South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.310917694935426, 149.10675337583916)"
1130929,2/03/2016,16:50,RURAL - MOLONGLO VALLEY,149.0765117,-35.2912999,YES,NO,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.291299904539166, 149.07651168464156)"
1047290,25/09/2012,9:28,LYONS,149.0659711,-35.34019838,NO,YES,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.34019837683168, 149.0659710656289)"
1
Ian_De_Oliveira 23 Сен 2018 в 04:34

2 ответа

Лучший ответ

Когда вы добавляете результаты в locations_daily, вы должны .append() указать новое местоположение, если crash_date уже существует. Это должно работать:

    if crash_date in locations_daily:
        locations_daily[crash_date].append(location)
    else:
        locations_daily[crash_date] = [location]
2
MLu 23 Сен 2018 в 01:42

Также обратите внимание, что вы на самом деле не используете csv.reader() в вашем скрипте. Используйте что-то вроде этого, чтобы воспользоваться преимуществом csv parser вместо разделения строк через запятую:

import csv

locations_daily={}

with open("data.csv") as f:
    reader = csv.reader(f, delimiter = ',')
    headers = reader.next()
    while True:
        try:
            line = reader.next()
        except StopIteration:
            break

        crash_date = line[1]
        location = (float(line[4]), float(line[5]))   # Also converting the lat/long to float

        if crash_date in locations_daily:
            locations_daily[crash_date].append(location)
        else:
            locations_daily[crash_date] = [location]

print(locations_daily)
0
MLu 23 Сен 2018 в 02:02