Re: Testing the Head of List Elements
- To: mathgroup at smc.vnet.net
- Subject: [mg25233] Re: [mg25200] Testing the Head of List Elements
- From: Ken Levasseur <Kenneth_Levasseur at uml.edu>
- Date: Sun, 17 Sep 2000 04:47:12 -0400 (EDT)
- Organization: UMass Lowell
- References: <200009150621.CAA10086@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Marshall:
It's clear exactly what form you want the input to take, so you probably
will need to tailor this to your needs. HeadSort produces a list of
items of the form {"ahead",list of expressions in data with head "ahead"}
This what you had in mind?
I'm sure the code could be improved on but I've got bring my son to
school now.
Ken Levasseur
UMass Lowell
http://faculty.uml.edu/klevasseur
In[13]:=
test = {{2}, "2", 2, two, Hold[1 + 1], {two}}
Out[13]=
{{2}, "2", 2, two, Hold[1 + 1], {two}}
In[14]:=
HeadSort[data_List] :=
Module[{t, types = Union[Head /@ data]},
Map[(t = #; {t, Select[data, (Head[#] == t) &]}) &, types]]
In[15]:=
HeadSort[test]
Out[15]=
{{Hold, {Hold[
1 + 1]}}, {Integer, {2}}, {List, {{2}, {two}}}, {String, {"2"}}, \
{Symbol, {two}}}
Marshall Bartlett wrote:
>
> I'm something of a newbie with Mathematica; please excuse my ignorance.
>
> I am looking for a clever way to select elements from a list based on their
> type (Head). Specifically, I want to extract all the elements of type
> dtring from a list of elements of different types (string, number,
> character, etc.).
>
> Any suggestions?
>
> --
> --------------------------------------------------------------
> Marshall Bartlett
> Department of Geology and Geophysics
> University of Utah
> mgb_news at hotmail.com
> --------------------------------------------------------------
- References:
- Testing the Head of List Elements
- From: "Marshall Bartlett" <mgb_news@hotmail.com>
- Testing the Head of List Elements