Re: Read word tables
Rameshwari wrote: > > Hi, > > I would like to read a ms-word document using python. > > Basically the word document contains number of tables and the rows > in each table do not have same number of columns. > > Does anyone have a sample code to read a table? > > Thank you > Best Regards, > Rameshwari The following code should return a list of list of lists (tables->table->rows->cells) for the active document in Microsoft Word. Warning! Untested code import win32com.client def GetTables(): app = win32com.client.Dispatch('Word.Application') doc = app.Documents[0] tables = [] for word_table in doc.Tables: table = [] for word_row in word_table.Rows: row = [cell.Range.Text for cell in word_row.Cells] table.append(row) tables.append(table) return tables -- Andrew Henshaw Georgia Tech Research Institute -- http://mail.python.org/mailman/listinfo/python-list
Re: league problem in python
"Ross" wrote in message news:d5cc0ec7-5223-4f6d-bab4-3801dee50...@r37g2000yqn.googlegroups.com... ... snip ... > I would like to create a simple program where the pro could enter in > how many people were in the league, the number of courts available, > and the number of weeks the schedule would run and then generate a > schedule where everybody played everybody else once and got the same > number of bye weeks, preferably spaced out evenly. > > How should I go about starting this problem...I'm feel like this is a > really simple problem, but I'm having writer's/coder's block. Can you > guys help? At least as a start, you want the round-robin tournament algorithm (see http://en.wikipedia.org/wiki/Round-robin_tournament). Here's some code that I use: ## begin code ### def roundrobin(teams, rounds=1): # if odd number of teams, add a "bye" team if len(teams) % 2: teams.append(None) mid = len(teams) // 2 for i in range(rounds): yield zip(teams[:mid], teams[mid:]) teams = teams[0:1] + teams[mid:mid+1] + teams[1:mid-1] + teams[mid+1:] + teams[mid-1:mid] ### test code ### if __name__ == '__main__': ROUNDS = 10 # two test cases - even number of teams and odd number of teams for teams in (range(6), range(5)): print "\nteams =", teams for round in roundrobin(teams, ROUNDS): print round ## end code ### And the output: teams = [0, 1, 2, 3, 4, 5] [(0, 3), (1, 4), (2, 5)] [(0, 4), (3, 5), (1, 2)] [(0, 5), (4, 2), (3, 1)] [(0, 2), (5, 1), (4, 3)] [(0, 1), (2, 3), (5, 4)] [(0, 3), (1, 4), (2, 5)] [(0, 4), (3, 5), (1, 2)] [(0, 5), (4, 2), (3, 1)] [(0, 2), (5, 1), (4, 3)] [(0, 1), (2, 3), (5, 4)] teams = [0, 1, 2, 3, 4] [(0, 3), (1, 4), (2, None)] [(0, 4), (3, None), (1, 2)] [(0, None), (4, 2), (3, 1)] [(0, 2), (None, 1), (4, 3)] [(0, 1), (2, 3), (None, 4)] [(0, 3), (1, 4), (2, None)] [(0, 4), (3, None), (1, 2)] [(0, None), (4, 2), (3, 1)] [(0, 2), (None, 1), (4, 3)] [(0, 1), (2, 3), (None, 4)] -- Andy -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking a Number for Palindromic Behavior
> wrote in message >news:63dea9e7-97af-4b20-aa0a-c762d9944...@a21g2000yqc.googlegroups.com... >On Oct 18, 4:20 pm, MRAB wrote: >> Benjamin Middaugh wrote: >> > Thanks to everyone who helped with my query on reversing integers. I >> > have one more simple problem I'm having trouble solving. I want to >> > check >> > a number for palindromic behavior (reading the same backwards and >> > forwards). So if I have an integer 1457 it can tell me this is not the >> > same from both ends but 1551 is. I think the simplest way would be to >> > work inwards from both ends checking digits for equality, but I don't >> > know enough (yet) to do this. >> >> > All help is much appreciated. >> >> It's a palindrome if it's the same as its reverse. You already know how >> to reverse it, and you should already know how to check whether two >> things are the same, so... :-) > >Something like: > >def is_palidrome (n): >return str(n) == ''.join (reversed (str(n))) > >which will return True if integer n is a palidromic or False >otherwise. I wouldn't normally provide a direct solution to this type of request; but since you have, may I suggest: def is_palindrome (n): return str(n) == str(n)[::-1] -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools.intersect?
"Raymond Hettinger" wrote in message news:fb1feeeb-c430-4ca7-9e76-fea02ea3e...@v23g2000pro.googlegroups.com... > [David Wilson] >> The problem is simple: given one or more ordered sequences, return >> only the objects that appear in each sequence, without reading the >> whole set into memory. This is basically an SQL many-many join. > > FWIW, this is equivalent to the Welfare Crook problem in David Gries > book, The Science of Programming, http://tinyurl.com/mzoqk4 . > > >> I thought it could be accomplished through recursively embedded >> generators, but that approach failed in the end. > > Translated into Python, David Gries' solution looks like this: > > def intersect(f, g, h): >i = j = k = 0 >try: >while True: >if f[i] < g[j]: >i += 1 >elif g[j] < h[k]: >j += 1 >elif h[k] < f[i]: >k += 1 >else: >print(f[i]) >i += 1 >except IndexError: >pass > > streams = [sorted(sample(range(50), 30)) for i in range(3)] > for s in streams: >print(s) > intersect(*streams) > > > Raymond Here's my translation of your code to support variable number of streams: def intersect(*s): num_streams = len(s) indices = [0]*num_streams try: while True: for i in range(num_streams): j = (i + 1) % num_streams if s[i][indices[i]] < s[j][indices[j]]: indices[i] += 1 break else: print(s[0][indices[0]]) indices[0] += 1 except IndexError: pass -- http://mail.python.org/mailman/listinfo/python-list
Re: finding most common elements between thousands of multiple arrays.
"mclovin" wrote in message news:c5332c9b-2348-4194-bfa0-d70c77107...@x3g2000yqa.googlegroups.com... > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating I up the count on the corrosponding dictionary element. I > then iterate through the dictionary and find the 25 most common > elements. > > the elements are initially held in a array within an array. so I am am > just trying to find the most common elements between all the arrays > contained in one large array. > my current code looks something like this: > d = {} > for arr in my_array: > -for i in arr: > #elements are numpy integers and thus are not accepted as dictionary > keys > ---d[int(i)]=d.get(int(i),0)+1 > > then I filter things down. but with my algorithm that only takes about > 1 sec so I dont need to show it here since that isnt the problem. > > > But there has to be something better. I have to do this many many > times and it seems silly to iterate through 2 million things just to > get 25. The element IDs are integers and are currently being held in > numpy arrays in a larger array. this ID is what makes up the key to > the dictionary. > > It currently takes about 5 seconds to accomplish this with my current > algorithm. > > So does anyone know the best solution or algorithm? I think the trick > lies in matrix intersections but I do not know. Would the following work for you, or am I missing something? For a 5Kx5K array, this takes about a tenth of a second on my machine. This code doesn't deal with the sub-array issue. # import numpy import time LOWER = 0 UPPER = 1024 SIZE = 5000 NUM_BEST = 4 # sample data data = numpy.random.randint(LOWER, UPPER, (SIZE,SIZE)).astype(int) time.clock() count = numpy.bincount(data.flat) best = sorted(zip(count, range(len(count[-NUM_BEST:] print 'time=', time.clock() print best -- http://mail.python.org/mailman/listinfo/python-list