Re: Flatten a list/tuple and Call a function with tuples
On Jul 25, 10:33 am, Jeff <[EMAIL PROTECTED]> wrote: > def flatten(obj): > if type(obj) not in (list, tuple, str): > raise TypeError("String, list, or tuple expected in > flatten().") > if len(obj) == 1: > if type(obj[0]) in (tuple, list): > return flatten(obj[0]) > else: > return [obj[0]] > else: > return [obj[0]] + flatten(obj[1:]) This seems to work fine only if the last object is the only one with the tuple or list. For example: >>> y = [(1,2),3,4] >>> y [(1, 2), 3, 4] >>> print flatten(y) [(1, 2), 3, 4] if the last line is changed to return flatten([obj[0]]) + flatten(obj[1:]) then it will unpack tuples/lists anywhere in the main collection being flattened: >>> y [(1, 2), 3, 4] >>> flatten(y) [1, 2, 3, 4] >>> z = [1,(2,3),4] >>> flatten(z) [1, 2, 3, 4] >>> x [1, 2, (3, 4)] >>> flatten(x) [1, 2, 3, 4] >>> k = [(1,2),(3,4)] >>> flatten(k) [1, 2, 3, 4] -- http://mail.python.org/mailman/listinfo/python-list
pyExcelerator and multiple worksheets
I'm using pyExcelerator to take a folder of CSV files and create Excel workbooks for all of them, then generate an Excel workbook with the data from all of them. Everything up until here works great; next, I make a second worksheet on the last workbook which has summary details regarding the previous worksheet. Here I run into a problem. The Excel form AVERAGE(Data!E5:E3053) where Data is the name of the worksheet being referenced (Summary is the name of the worksheet where the formula is entered) isn't recognized when I call Formula(). Currently I'm just writing the string =AVERAGE(Data!E5:E3053) into the cell, but this doesn't get automatically evaluated; there are multiple instances of this formatting in use and when used with a larger source folder, evaluating each cell by hand becomes a significant problem. Without any documentation for pyExcelerator I turned to the examples provided, but none of them seemed to use cross-worksheet formulas. Does anybody know how to fix this, or alternatively know a different package I could use? I found xlrd but as that is read-only it doesn't seem to solve the problem, unless there's some insane and creative way to use xlrd and pyExcelerator together to solve this. Thanks in advance! --Aneesh -- http://mail.python.org/mailman/listinfo/python-list