[Date Index]
[Thread Index]
[Author Index]
Re: Selecting by first element of each list
*To*: mathgroup at smc.vnet.net
*Subject*: [mg48167] Re: Selecting by first element of each list
*From*: Bill Rowe <readnewsciv at earthlink.net>
*Date*: Fri, 14 May 2004 20:59:57 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
On 5/14/04 at 12:12 AM, camartin at snet.net wrote:
>I'm trying to learn to handle a flat file kind of database using
>Mathematica. An example is the list of lists below:
>{{2,3,4,5,77},{2,4,5,66,77},{3,4,7,8,90,6},{3,5,6,7,8,0},{3,45,67,
>77,12}}
>It's actually more complicated than this but good enough for
>illustrative purposes. The first element in each list is actually
>an identifier, like an account number. I want to create a list of
>lists with each account, that is,
>{{{2,3,4,5,77},{2,4,5,66,77}},
>{{3,4,7,8,90,6},{3,5,6,7,8,0},{3,45,67,77,12}}}.
>When I use Select with an anonymous function such as
>Select[list1,#1[[1]] = = #2[[1]]&]
There are two problems with the syntax above. First, there is a space between the two "=" characters. Second, Select checks each element of the list against the specified criteria. Select does not compare one element with another.
One way to achieve what you want would be to create your own selection function, i.e.,
accountSelect[data_,account_]:=Select[data,First@# == account&]
then
accountSelect[data,#]&/@Union[First/@data]
will do what you want.
FWIW, I've written a package for my use containing similar functions. I've thought about submitting it to MathSource but haven't taken time to write examples showing usage of the functions I've created. If you are interested, I would be willing to send it to you offline as is.
In my case, the first row of my data arrays is a list of undefined symbols. Using your example above, I might add as the first row {id, value1, value2, value3, value4}.
This does two things for me. First when I display the data array in either TraditionalForm or TableForm, I get a nice display with something that identifies the purpose of each column. Second, I can use the symbols in the first row to refer a particular column, i.e., I can write things such as
SelectRows[data, id, 2]
to select all rows of data with a 2 in the column headed by id. With this I find it much easier to manipulate the data and determine what my code is doing with the data.
--
To reply via email subtract one hundred and four
Prev by Date:
**Re: Selecting by first element of each list**
Next by Date:
**Re: Selecting by first element of each list**
Previous by thread:
**Re: Selecting by first element of each list**
Next by thread:
**RE: Selecting by first element of each list**
| |