'Stuck trying to iterate through a list in Python to add extracted variables to a dictionary [duplicate]
I have a list that contains data for different rivers and want to extract only the name and flow rates and insert those in a dictionary, with the name as the key and a list of the flowrates as the values. To do this I made a another list of each row that was divided better and worked from there. However, when I run the code I encounter an error on append dictionary line that simply states KeyError: 'Avon', which I don't understand so I haven't been able to find a solution.
river_info = {}
flow_info = []
river_data = [
"Avon,27/05/2020 12:00:00 a.m.,1.634",
"Avon,27/05/2020 1:00:00 a.m.,1.628",
"Hakatere,27/05/2020 12:00:00 a.m.,9.813",
"Hakatere,27/05/2020 1:00:00 a.m.,9.813",
"Rangitata,27/05/2020 12:00:00 a.m.,50.699",
"Rangitata,27/05/2020 1:00:00 a.m.,50.790",
"Waimakariri,27/05/2020 12:00:00 a.m.,73.598",
"Waimakariri,27/05/2020 1:00:00 a.m.,73.236",
]
i = 0
for i in range(len(river_data)):
river = river_data[i].split(",")
river_name = str(river[0])
river_flow = float(river[-1])
flow_info.append(river_flow)
river_info[river_name].append(flow_info)
i += 1
print(flow_info)
Solution 1:[1]
You will find the setdefault() function useful for this.
river_info = dict()
flow_info = list()
river_data = [
"Avon,27/05/2020 12:00:00 a.m.,1.634",
"Avon,27/05/2020 1:00:00 a.m.,1.628",
"Hakatere,27/05/2020 12:00:00 a.m.,9.813",
"Hakatere,27/05/2020 1:00:00 a.m.,9.813",
"Rangitata,27/05/2020 12:00:00 a.m.,50.699",
"Rangitata,27/05/2020 1:00:00 a.m.,50.790",
"Waimakariri,27/05/2020 12:00:00 a.m.,73.598",
"Waimakariri,27/05/2020 1:00:00 a.m.,73.236",
]
for rd in river_data:
river, *_, fs = rd.split(',')
flow = float(fs)
river_info.setdefault(river, []).append(flow)
flow_info.append(flow)
print(river_info)
print(flow_info)
Output:
{'Avon': [1.634, 1.628], 'Hakatere': [9.813, 9.813], 'Rangitata': [50.699, 50.79], 'Waimakariri': [73.598, 73.236]}
[1.634, 1.628, 9.813, 9.813, 50.699, 50.79, 73.598, 73.236]
Solution 2:[2]
You should just check if your river name (as key) is present already in the dictionary. If yes, then append flow to it's value, else add a new entry with key as river name as value as list of flow info:
river_info = {}
river_data = [
"Avon,27/05/2020 12:00:00 a.m.,1.634",
"Avon,27/05/2020 1:00:00 a.m.,1.628",
"Hakatere,27/05/2020 12:00:00 a.m.,9.813",
"Hakatere,27/05/2020 1:00:00 a.m.,9.813",
"Rangitata,27/05/2020 12:00:00 a.m.,50.699",
"Rangitata,27/05/2020 1:00:00 a.m.,50.790",
"Waimakariri,27/05/2020 12:00:00 a.m.,73.598",
"Waimakariri,27/05/2020 1:00:00 a.m.,73.236",
]
for i in range(len(river_data)):
river = river_data[i].split(",")
river_name = str(river[0])
river_flow = float(river[-1])
if river_name in river_info:
river_info[river_name].append(river_flow)
else:
river_info[river_name] = [river_flow]
print(river_info)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Albert Winestein |
| Solution 2 | Devesh |
