[Tutor] dictionary of lists

Chris Stinemetz chrisstinemetz at gmail.com
Wed Jun 3 22:39:49 CEST 2015


>
>> Resetting execution engine
>> Running
>> C:\Users\cs062x\Desktop\python\projects\PanHandle\PanHandle\PanHandle.py
>> The Python REPL process has exited
>
>
> That's slightly unusual. How are you running this?
>

I am running it with Microsoft Visual Studio Community 2013 using
Python Tools for Visual Studio


>
> Now, what I don't know, is what you are trying to do.
> Are you trying to append the float to the list?
> Or to replace the list with the float?
> Or to add the float to the value of the first(or last?)
> element in the list - if it exists
> (and if it doesn't? Then what?)
>
>

Although I am certain it is not very efficient I was able to
accomplish what I wanted with the following code I wrote:

import os
import pprint
import csv
from collections import defaultdict

print_map =  {'MOU':0, 'Call_Att':1, 'Device':2}
header = ['IMEI','MOUs','Call_Att','Device']

path = 'C:/Users/cs062x/Desktop/Panhandle'

os.chdir(path)
running_MOU = {}
call_attempts = {}
d = defaultdict(list)
for fname in os.listdir('.'):
    with open (fname) as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
        next(spamreader)
        for row in spamreader:

            if row[8]:
                device = row[36]
                Elapsed_Mins = float(row[7])
                IMEI = row[8].replace("'", "")

                if IMEI in running_MOU.keys():
                    running_MOU[IMEI] += Elapsed_Mins
                else:
                    running_MOU[IMEI] = Elapsed_Mins

                if IMEI in call_attempts.keys():
                    call_attempts[IMEI] += 1
                else:
                    call_attempts[IMEI] = 1

                # if key matches append mou else append 0.
                d[IMEI] = [running_MOU[IMEI]]
                d[IMEI].append([call_attempts[IMEI]])
                d[IMEI].append([device])


print ",".join(header)
for k,v in sorted(d.items()):
    print k, ",", d[k][print_map['MOU']],",",
d[k][print_map['Call_Att']][0],",", d[k][print_map['Device']][0]

print "complete"


More information about the Tutor mailing list