Re: [Tutor] dictionary of lists
Chris Stinemetz wrote: > 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(): For big dicts in Python 2 the test key in some_dict.keys() is indeed very inefficient as it builds a list of keys first and then performs a linear scan for the key. Much better: key in some_dict This test avoids building the list and can also use an efficient lookup algorithm that is independent of the size of the dict. > 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" Here's an alternative that uses only one dict: import csv import os import sys header = ['IMEI', 'MOUs', 'Call_Att', 'Device'] path = 'C:/Users/cs062x/Desktop/Panhandle' d = {} for fname in os.listdir(path): with open(os.path.join(path, 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 d: record = d[IMEI] record[1] += elapsed_mins record[2] += 1 else: d[IMEI] = [IMEI, elapsed_mins, 1, device] writer = csv.writer(sys.stdout) writer.writerow(header) writer.writerows(sorted(d.itervalues())) print "complete" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionary of lists
On Thu, Jun 4, 2015 at 2:30 AM, Peter Otten <__pete...@web.de> wrote: > Chris Stinemetz wrote: > >> 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(): > > For big dicts in Python 2 the test > > key in some_dict.keys() > > is indeed very inefficient as it builds a list of keys first and then > performs a linear scan for the key. Much better: > > key in some_dict > > This test avoids building the list and can also use an efficient lookup > algorithm that is independent of the size of the dict. > >> 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" > > Here's an alternative that uses only one dict: > > import csv > import os > import sys > > header = ['IMEI', 'MOUs', 'Call_Att', 'Device'] > > path = 'C:/Users/cs062x/Desktop/Panhandle' > > d = {} > for fname in os.listdir(path): > with open(os.path.join(path, 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 d: > record = d[IMEI] > record[1] += elapsed_mins > record[2] += 1 > else: > d[IMEI] = [IMEI, elapsed_mins, 1, device] > > writer = csv.writer(sys.stdout) > writer.writerow(header) > writer.writerows(sorted(d.itervalues())) > > print "complete" Peter - Thank you for showing me how to do this with one dictionary and a more efficient method to lookup dictionary keys. I originally attempted to accomplish this by using one dictionary but could not find a good example that is why I used the defaultdict module. Your approach sped the parsing time up from about 3 minutes to about 15 seconds! Very cool. Thanks, Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Image library
hello , sir i wanted to know that how can i show or display a simple image using python 3.4The thing is that i want to know that there is no image module or library located in the library folder under python 3.4.?sir, please help me out with this basic step.. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Image library
In a message of Thu, 04 Jun 2015 14:09:43 -, abhijeet...@yahoo.in writes: >hello , sir i wanted to know that how can i show or display a simple image >using python 3.4The thing is that i want to know that there is no image module >or library located in the library folder under python 3.4.?sir, please help me >out with this basic step.. I use Pillow. But you may also have to install some libraries in order to show jpegs, etc. See the list here. http://pillow.readthedocs.org/en/latest/installation.html Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Image library
On 04/06/15 15:09, abhijeet...@yahoo.in wrote: i wanted to know that how can i show or display a simple image using python 3.4 The Tkinter package in the standard library includes a Canvas widget which can display various forms of image. It depends what format the image is in. You will probably need to create a PhotoImage object then inset that into the canvas. The thing is that i want to know that there is no image module or library > located in the library folder under python 3.4.? There is no specific image folder but there is the Tkinter GUI tookit There are several 3rd party toolkits you can downloasd too such as Pillow and imageMagick (for manipulating/converting images). Both include very basic display capability. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Image library
On 04/06/15 16:51, Alan Gauld wrote: There is no specific image folder but there is the Tkinter GUI tookit Oops, I meant "image module"... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble using bioread to convert files from .acq to .mat (fwd)
Missed sending this to the list. Sorry. In a message of Wed, 03 Jun 2015 08:56:48 -0400, Ila Kumar writes: >Laura, that was it! Thank you so much. You are most welcome. > >However, now I am confused about what I need to type into the Command >prompt window (on a 64-bit windows computer, using python 2.7) in order to >convert a folder of data files. Does anyone know the proper code to do this? I am not an expert in bioread, alas. I'm an expert in "ARGH! easy_install won't install this module!!!" But there is an example in the github directory for bioread. Maybe it does what you want? No promises. If it doesn't mailing the package author directly may be your best bet. If you talk to him, ask him to put a line in his pypi file saying that you need to install https://pypi.python.org/pypi/ez_setup if you get an ez_setup not found error, please. At any rate, here is the example. I haven't used it. https://github.com/njvack/bioread/blob/master/examples/api_demo.py Laura --- End of Forwarded Message ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor