Converting List Of Lists In Dictionary Python
I'm facing the following problem. I have a list of lists obtained from a remote URL with the following code: import csv import urllib.request text_url = 'https://www.emidius.eu/fd
Solution 1:
Using csv.DictReader
and dict.setdefault
Ex:
import csv
d = {}
reader = csv.DictReader(lines, delimiter='|')
for row in reader: #Iterate Each row
for k, v in row.items(): #Iterate Key-Value
d.setdefault(k, []).append(v)
Solution 2:
Try this,
>>>result_dict = {}>>>for idx, key inenumerate(a):
for val in b:
result_dict.setdefault(key, []).append(val[idx])
Output:
>>> result_dict
{'#EventID': ['quakeml:eu.ahead/event/18990105_0245_000', 'quakeml:eu.ahead/event/18990118_2048_000', 'quakeml:eu.ahead/event/18990122_0956_000', 'quakeml:eu.ahead/event/18990131_1112_000', 'quakeml:eu.ahead/event/18990131_2345_000'], 'Time': ['1899-01-05T02:45:--', '1899-01-18T20:48:--', '1899-01-22T09:56:--', '1899-01-31T11:12:--', '1899-01-31T23:45:--'], 'Latitude': ['41.500', '46.180', '37.200', '66.300', '60.100'], 'Longitude': ['13.783', '14.500', '21.600', '-19.900', '5.500'], 'Depth/km': ['', '4.8', '', '', '30'], 'Author': ['AHEAD', 'AHEAD', 'AHEAD', 'AHEAD', 'AHEAD'], 'Catalog': ['SHEEC', 'SHEEC', 'SHEEC', 'SHEEC', 'SHEEC'], 'Contributor': ['CPTI04', 'RIBA982', 'PAPA003', 'AMBSI000', 'FEN007'], 'ContributorID': ['1309', '', '', '', ''], 'MagType': ['Mw', 'Mw', 'Mw', 'Mw', 'Mw'], 'Magnitude': ['4.63', '4.51', '6.50', '5.80', '4.60'], 'MagAuthor': ['SHEEC', 'SHEEC', 'SHEEC', 'SHEEC', 'SHEEC'], 'EventLocationName': ['Pignataro', 'Vodice Brnik', 'Kyparissia', '[N. Iceland]', '[Biornafjorden]']}
Solution 3:
One naive option is this:
l = [["a","b","c"],[1,2,3],[4,5,6],[7,8,9]]
d = {k:[] forkin l[0]}
foriin l[1:]:
dummy = {k:v fork,v inzip(l[0],i)}
forkin d.keys():
d[k].append(dummy[k])
Solution 4:
list can be rotated 90 degrees by zip()
d = {key:val for key, val in zip(my_list[0], zip(*my_list[1:]))}
Solution 5:
Another way to solve your problem without using dictionaries would be to load the CSV file into a Pandas data frame:
import pandas as pd
import urllib.request
text_url = 'https://www.emidius.eu/fdsnws/event/1/query?starttime=1899-01-01T00:00:00&endtime=1899-01-31T23:59:59&minmag=4&maxmag=9&orderby=time-asc&limit=100&format=text'with urllib.request.urlopen(text_url) as response:
df = pd.read_csv(response, sep='|')
Now the data is in a structured format:
>>> df
#EventID ... EventLocationName0 quakeml:eu.ahead/event/18990105_0245_000 ... Pignataro
1 quakeml:eu.ahead/event/18990118_2048_000 ... Vodice Brnik
2 quakeml:eu.ahead/event/18990122_0956_000 ... Kyparissia
3 quakeml:eu.ahead/event/18990131_1112_000 ... [N. Iceland]
4 quakeml:eu.ahead/event/18990131_2345_000 ... [Biornafjorden]
[5 rows x 13 columns]
>>> df['#EventID']
0 quakeml:eu.ahead/event/18990105_0245_0001 quakeml:eu.ahead/event/18990118_2048_0002 quakeml:eu.ahead/event/18990122_0956_0003 quakeml:eu.ahead/event/18990131_1112_0004 quakeml:eu.ahead/event/18990131_2345_000
Name: #EventID, dtype: object>>> df.Latitude * df.Longitude
0571.99451669.61002803.52003 -1319.37004330.5500
dtype: float64
Post a Comment for "Converting List Of Lists In Dictionary Python"