python 3.6 Thanks in advace :) What\'s allowed? is the exhaustive list of things
ID: 3854934 • Letter: P
Question
python 3.6
Thanks in advace :)
What's allowed? is the exhaustive list of things you can use on the projec all basic expressions/operators, indexing/slicing all basic statements: assignment, selection, and loop statements, break/continue, return here is the exhaustive list of things you can use on the project. . functions: len), range(), min(), max(), sanitize() (you'll write sanitize!) file reading: open), close), read(), readline(), readlines), with syntax dictionaries: all methods listed in our slides on that one chart. methods: ists: remove), insert), append(), extend) pop() strings: strip), split), join), remove(), insert), lower) sorted (), sort(), reversed (), reverse() This means that... ·you can't call anything not listed above. Focus on applying these functions to solve the task. you can't import any modules for this project. (so you can't import csv either-but it isn't that helpful)Explanation / Answer
import ast
def createDBfromString(s):
d=ast.literal_eval(s)
return d
def read_file(filename):
l=[]
header=[]
storm=[]
db=''
import csv
with open(filename, 'rt', encoding='utf8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
l.append(row)
n=len(l)
for i in range(n):
if i==0:
t=str(l[i])[2:-2]
header=list(t.split(','))
elif i==(n-1):
t=str(l[i])[2:-2]
db+=t
else:
t=str(l[i])[2:-2]
storm.append(list(t.split(',')))
return header,storm,createDBfromString(db)
def add_storm(db,year,name,category,deaths,damage):
if year in db:
db[year].append((name,category,deaths,damage))
else:
db[year]=(name,category,deaths,damage)
def merge_databases(x, y):
"""Given two dicts, merge them into a new dict as a shallow copy."""
z = x.copy()
z.update(y)
return z
def storms_by_names(db,name):
val=db.values()
c=[]
d={}
f=[x for v in val for x in v if type(v)==list]
for j in f:
if name in j[0]:
c.append(j)
for l,m in db.items():
if c[0] in m:
d[l]=c[0]
return d
def storms_by_years(db,years):
d={}
for i in years:
if i in db.keys():
d[i]=db[i]
return d
header,storm,db=read_file('file2.csv')
print(storms_by_names(db,input()))
print (storms_by_years(db,[1950,1954]))
#print ('header : ',header,' DB : ',db,' Storm : ',storm)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.