Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Picking Off Lists That Have No Numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99541] Re: [mg99471] Picking Off Lists That Have No Numbers
  • From: Gregory Lypny <gregory.lypny at videotron.ca>
  • Date: Thu, 7 May 2009 06:38:24 -0400 (EDT)
  • References: <200905060925.FAA01807@smc.vnet.net>

Darn, that is good!

On Wed, May 6, 2009, at 5:41 PM, DrMajorBob wrote:

> Or in one statement:
>
> mixed = {"NA", 2.3, 3/8, "NA", "NA", "NA"};
> {first, last} = Select[mixed, NumericQ][[{1, -1}]]
>
> {2.3, 3/8}
>
> Bobby
>
> On Wed, 06 May 2009 15:56:59 -0500, Gregory Lypny <gregory.lypny at videotron.ca 
> > wrote:
>
>> Thanks Bobby,
>>
>> It's crystal now or almost so.  Painful yet kind of fun getting  
>> there.  For example, I came up with this way to identify the  
>> location of the first and last numerical elements in a mixed list  
>> of strings and numbers.
>>
>> Regards,
>>
>> 	G.
>>
>> posOfFirstNumericalObs = First[Position[myMixedList, _?NumericQ]] 
>> [[1]]
>>
>> posOfLastNumericalObs = Last[Position[myMixedList, _?NumericQ]][[1]]
>>
>>
>> On Wed, May 6, 2009, at 4:46 PM, DrMajorBob wrote:
>>
>>> All the "wrong" answers are caused by bad syntax or arguments.
>>>
>>> FreeQ[#, _Number] & /@ {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}}
>>>
>>> and
>>>
>>> MemberQ[#, _Number] & /@ {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}}
>>>
>>> both fail because Number is not the Head for numbers:
>>>
>>> Map[Head, {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}}, {2}]
>>>
>>> {{String, Real, Rational}, {String, String, String}}
>>>
>>> Head /@ {2.3, 2/3, 1 + I, Pi}
>>>
>>> {Real, Rational, Complex, Symbol}
>>>
>>> so _Number doesn't match any of the elements.
>>>
>>> What you need instead is something like
>>>
>>> FreeQ[#, _?NumericQ] & /@ {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}}
>>>
>>> {False, True}
>>>
>>> NumericQ /@ {2.3, 2/3, 1 + I, Pi}
>>>
>>> {True, True, True, True}
>>>
>>> All the other problems are similar.
>>>
>>> Bobby
>>>
>>> On Wed, 06 May 2009 04:25:48 -0500, Gregory Lypny <gregory.lypny at videotron.ca 
>>> > wrote:
>>>
>>>> Hi everyone,
>>>>
>>>> In a previous thread, "Select and Cases Give Different Answers," I
>>>> discussed a bug, confirmed by others on the list and which Daniel
>>>> Lichtblau of Wolfram said has been fixed in the development kernel,
>>>> and I wonder whether we have the same problem here.
>>>>
>>>> I want to know if a list has no numbers in it.  Here I get the  
>>>> wrong
>>>> answer for the first list.
>>>>
>>>> FreeQ[#, _Number] & /@ {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}}
>>>>
>>>> yields {True, True}
>>>> And same here.
>>>>
>>>> MemberQ[#, _Number] & /@ {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}}
>>>>
>>>> {False, False}
>>>>
>>>> If I use Real or Rational for the criterion, I get the right  
>>>> answers,
>>>> but that's no good if you have some lists that are mixtures of  
>>>> strings
>>>> and reals and others that are mixtures of strings and rationals.
>>>>
>>>> FreeQ[#, _Real] && FreeQ[#, _Rational]
>>>> & /@ {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}, {"NA", 2.3, "NA"},  
>>>> {"NA",
>>>> 2.3, 9.4}}
>>>>
>>>> Now this works.
>>>>
>>>> Count[#, _String] == Length[#] & /@ {{"NA", 2.3, 3/8}, {"NA", "NA",
>>>> "NA"}}
>>>>
>>>> {False, True}
>>>>
>>>> But this does not!
>>>>
>>>> Count[#, _Number] == 0 & /@ {{"NA", 2.3, 3/8}, {"NA", "NA", "NA"}}
>>>>
>>>> {True, True}
>>>>
>>>> I don't mean to be a pain, but it is important in my sample  
>>>> selection
>>>> procedures to be able to make the distinction between strings and
>>>> numbers, so I'd be interested in knowing whether I'm  
>>>> misunderstanding
>>>> properties such as Number or Numeric (don't really know the
>>>> difference) or this is a bug.
>>>>
>>>>
>>>> 	Gregory
>>>>
>>>
>>>
>>>
>>> --DrMajorBob at bigfoot.com
>>
>
>
>
> -- 
> DrMajorBob at bigfoot.com



  • Prev by Date: Re: Given a matrix, find position of first non-zero element
  • Next by Date: two graph problems in Adjacency types
  • Previous by thread: Re: Picking Off Lists That Have No Numbers
  • Next by thread: Re: Picking Off Lists That Have No Numbers