<jupyter_start><jupyter_text>"Working with Data Structures"> "Looking at Maine COVID data using only dictionaries, list comprehension and matplotlib."- toc: false- badges: true- comments: true- author: <NAME>- categories: [learning, python]<jupyter_code>import csv
import os

os.chdir('/Users/antoniojurlina/Projects/learning_python/data/')

file_location = "MaineCovidData.txt"

countycases = []

with open(file_location) as co:
    co_reader = csv.reader(co, delimiter='\t')
    header=next(co_reader)
    for line in co_reader:
        countycases.append(line)

county = [countycases[x] for x in range(0, 80, 5)]
county = [item for l in county for item in l]
county = [i.replace(' County', '') for i in county]

cases = [countycases[x] for x in range(1, 80, 5)]
cases = [item for l in cases for item in l]
cases = [int(i.replace(',', '')) for i in cases]

cases_new = [countycases[x] for x in range(2, 80, 5)]
cases_new = [item for l in cases_new for item in l]
cases_new = [int(i.replace('+', '')) for i in cases_new]

deaths = [countycases[x] for x in range(3, 80, 5)]
deaths = [item for l in deaths for item in l]
deaths = [int(i.replace(',', '')) for i in deaths]

deaths_new = [countycases[x] for x in range(4, 80, 5)]
deaths_new = [item for l in deaths_new for item in l]
deaths_new = [int(i.replace('+', '')) for i in deaths_new]

cases_updated = [a + b for a, b in zip(cases, cases_new)]
deaths_updated = [a + b for a, b in zip(deaths, deaths_new)]

cases_by_county = dict(zip(county, cases))
cases_by_county_updated = dict(zip(county, cases_updated))
deaths_by_county_updated = dict(zip(county, deaths_updated))
cases_deaths_by_county = dict(zip(county, (zip(cases_updated, deaths_updated))))<jupyter_output><empty_output><jupyter_text>Bar chart of the second dictionary showing the cases updated with the increment:<jupyter_code>import matplotlib.pyplot as plt

res = dict(reversed(list(cases_by_county_updated.items())))

%matplotlib inline
plt.style.use('ggplot')


x_pos = [i for i, _ in enumerate(res.keys())]

plt.bar(x_pos, res.values(), color='orange')
plt.xlabel("County")
plt.ylabel("Cases")
plt.title("Maine Covid Cases by County")

plt.xticks(x_pos, res.keys(), rotation =90)

plt.show()<jupyter_output><empty_output><jupyter_text>Bar chart that shows the deaths updated with the daily increment:<jupyter_code>res = dict(reversed(list(deaths_by_county_updated.items())))

%matplotlib inline
plt.style.use('ggplot')


x_pos = [i for i, _ in enumerate(res.keys())]

plt.bar(x_pos, res.values(), color='red')
plt.xlabel("County")
plt.ylabel("Deaths")
plt.title("Maine Covid Deaths by County")

plt.xticks(x_pos, res.keys(), rotation =90)

plt.show()<jupyter_output><empty_output>