Re: Help with lists

• Subject: [mg2809] Re: [mg2782] Help with lists
• From: brucec (Bruce Carpenter)
• Date: Tue, 19 Dec 1995 02:41:12 -0500
• Approved: usenet@wri.com
• Distribution: local
• Newsgroups: wri.mathgroup
• Organization: Wolfram Research, Inc.

```>        Hi! I'm having problems with the lists I try and manipulate.
>I set a list as something and then try and get mathematica to pull out a
>number in a range and it doesn't return anything. Like so;
>
>In[1]:  list = {-5,-4,-3,-2,-1,0,1,2,3,4,5}
>In[2]:  Count[list,{-2,2}]
>
>What I'm trying to get the program to do is Count the values in the list
>between certain values. I need to be able to do this over a negative and
>positive domain. Could someone let me know how to do this, or perhaps a
>way to get around the limitations of the program?
>Thanks
>Andre

You could use the built-in Interval arithmetic along with the pattern
matching construct of Cases(or Count, for that matter):

In[25]:=
??Interval*
Interval             IntervalMemberQ      IntervalUnion
IntervalIntersection
In[26]:=
mylist = {-5,-4,-3,-2,-1,0,1,2,3,4,5};
Cases[list,a_/;IntervalMemberQ[Interval[{-2,2}],a]]
Out[27]=
{-2, -1, 0, 1, 2}
In[28]:=
Count[list,a_/;IntervalMemberQ[Interval[{-2,2}],a]]
Out[28]=
5

Cheers,
Bruce Carpenter

```

• Prev by Date: Distribute
• Next by Date: Function?
• Previous by thread: Re: Help with lists
• Next by thread: Re: Help with lists