Re: Using Map or Thread over selected parts of nested lists
- To: mathgroup at smc.vnet.net
- Subject: [mg95924] Re: Using Map or Thread over selected parts of nested lists
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 29 Jan 2009 06:01:41 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <glpfhn$knq$1@smc.vnet.net>
In article <glpfhn$knq$1 at smc.vnet.net>, "Paul Ellsmore" <paul.ellsmore at nanion.co.uk> wrote: [snip] > 1) My GUI allows the user to select some data files to work with. I use > the code: > > source = SystemDialogInput[FileOpen] > > This returns the full path for each file selected by the user - very > convenient and user-friendly. My problem is that "source" is returned as a > string (ie an atom) if one file is selected, but a nested list if multiple > files are selected. I have similar instances in many parts of my program > where the user might be working with one list of data or multiple lists, and > I think the same issue will arise. So I would like to know if there is a way > of forcing (in this case) "source" to be returned as a nestedlist type, even > if the length of that list is one? One possible way is as follows: In[1]:= source = Flatten[{SystemDialogInput["FileOpen"]}] Out[1]= {"/Users/jeanmarc/Documents/DCP_1175.JPG"} In[2]:= source = Flatten[{SystemDialogInput["FileOpen"]}] Out[2]= {"/Users/jeanmarc/Documents/DCP_1176.JPG", \ "/Users/jeanmarc/Documents/DCP_1178.JPG"} > 2) I have data in the format: > > List1={{header,header},{header,header},{header,header},{frequency1, > complexnumber1},{frequency2, complexnumber2},{frequency3, complexnumber4}.} > > On very many occasions I want to deal with : > > List2={{frequency1, realpart1},{frequency2, realpart2},{frequency3, > realpart4}.} > > Or some other trivial transform of complexnumbern. I have been through the > VirtualBook, and Documentation centre, and tried various forms of Map and > Thread, but I cannot find a way of "mapping" or "threading" a function like > Re[expr] over one element of part of a list, as in: > > List2={{frequency1, Re[complexnumber1]},{frequency2, > Re[complexnumber2]},{frequency3, Re[complexnumber4]}.} > > If someone could give me a pointer to the correct syntax, I would be very > grateful. > > At present, I am getting the results I want by using Ifs and Do loops, to > deconstruct and reconstruct my lists, but I feel that is the wrong way to > approach things - I want to use Mathematica's strengths, not force it to use > a C type approach, just because that's what I am comfortable with. > > Any suggestions gratefully received, Here is a Mathematica way of doing it: Cases[list1, {r_Real, c_Complex} :> {r, Re[c]}] For instance, In[1]:= list1 = {{header, header}, {header, header}, {header, header}, {RandomReal[], RandomComplex[]}, {RandomReal[], RandomComplex[]}, {RandomReal[], RandomComplex[]}, {RandomReal[], RandomComplex[]}} Out[1]= {{header, header}, {header, header}, {header, header}, {0.675675, 0.225041+ 0.928453 I}, {0.667356, 0.820341+ 0.0827457 I}, {0.171542, 0.619974+ 0.143676 I}, {0.4259, 0.346343+ 0.190333 I}} In[2]:= Cases[list1, {r_Real, c_Complex} :> {r, Re[c]}] Out[2]= {{0.675675, 0.225041}, {0.667356, 0.820341}, {0.171542, 0.619974}, {0.4259, 0.346343}} Regards, --Jean-Marc