Can I search a list for a range of values?

M

MrPink

I have a list like so:

a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]

I would like to get a subset from the list with value between 10 and
20 (inclusive).

b = [10,13,15,14,20]

Is there a way to do this with a list or other data type?

Thanks,
 
C

Chris Angelico

I have a list like so:

a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]

I would like to get a subset from the list with value between 10 and
20 (inclusive).

b = [10,13,15,14,20]

Is there a way to do this with a list or other data type?

Try a list comprehension:
a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]
[i for i in a if i>=10 if i<=20]
[10, 20, 15, 13, 14]

This preserves order from the original list. I don't know what order
your result list is in, but you can always rejig things after.

ChrisA
 
I

Ian Kelly

I have a list like so:

a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]

I would like to get a subset from the list with value between 10 and
20 (inclusive).

b = [10,13,15,14,20]

Is there a way to do this with a list or other data type?

Use a list comprehension:

b = [x for x in a if 10 <= x <= 20]

Cheers,
Ian
 
T

Tim Chase

Try a list comprehension:
a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]
[i for i in a if i>=10 if i<=20]
[10, 20, 15, 13, 14]

The double-if is new to me. I thought it was an error when I
first saw it, but it seems to be legit syntax (or at least one
that 2.7 tolerates, intentionally or otherwise...). I think I'd
make it clearer with either

[i for i in a if i>=10 and i<=20]

or even more clearly:

[i for i in a if 10 <= i <= 20]

-tkc
 
C

Chris Angelico

The double-if is new to me.  I thought it was an error when I first sawit,
but it seems to be legit syntax (or at least one that 2.7 tolerates,
intentionally or otherwise...).  I think I'd make it clearer with either

Yeah, it's legal because you can nest fors and ifs in a list comp.
Stylistic difference, I used "if" instead of "and" because there's no
way that it could be a bitwise and. (It's a cross-language safety net
that I sometimes use.) Although the 10<=i<=20 option is definitely
superior to both.

ChrisA
 
I

Ian Kelly

Yeah, it's legal because you can nest fors and ifs in a list comp.
Stylistic difference, I used "if" instead of "and" because there's no
way that it could be a bitwise and. (It's a cross-language safety net
that I sometimes use.) Although the 10<=i<=20 option is definitely
superior to both.

At least in Python, there is no way that "and" could be a bitwise and
either, since it cannot be overloaded.
 
I

Ian Kelly

Try a list comprehension:
a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]
[i for i in a if i>=10 if i<=20]

[10, 20, 15, 13, 14]

The double-if is new to me.  I thought it was an error when I first sawit,
but it seems to be legit syntax (or at least one that 2.7 tolerates,
intentionally or otherwise...).  I think I'd make it clearer with either

 [i for i in a if i>=10 and i<=20]

or even more clearly:

 [i for i in a if 10 <= i <= 20]

As long as we're nitpicking, I'll point out that "i" is an
inappropriate variable name here, since it is normally used to denote
indices, not data. That's why I used "x" in my response instead. ;-)

Cheers,
Ian
 
T

Tim Chase

or even more clearly:

[i for i in a if 10<= i<= 20]

As long as we're nitpicking, I'll point out that "i" is an
inappropriate variable name here, since it is normally used to
denote indices, not data. That's why I used "x" in my
response instead. ;-)

Depending on your historical programming-language baggage, "i" is
usually either an index or integer data, and since the source was
a list of integers, "i" didn't seem inappropriate. Same for
other common data-types:

[f for f in (1.1, 2.2, 3.3) if 2.0 <= f < 3.0]
[s for s in ("cat", "hat", "mat") if "bat" < s < "fat"]
[c for c in "hello, world!" if 'a' <= c <= 'z']

-tkc
 
D

Dennis Lee Bieber

I have a list like so:

a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]

I would like to get a subset from the list with value between 10 and
20 (inclusive).

b = [10,13,15,14,20]

Is there a way to do this with a list or other data type?
a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]
b = [ itm for itm in a if itm >= 10 and itm <= 20 ]
b [10, 20, 15, 13, 14]
 
T

Troy S

Can something like this be done with dictionarys?

For example, these are the keys in the dictionary from the call: dict.keys()

['20110601', '20110604', '20110608', '20110611', '20110615',
'20110618', '20110622', '20110625', '20110629', '20110702',
'20110706','20110709', '20110713', '20110716', '20110720', '20110723',
'20110727', '20110730', '20110803', '20110806', '20110810','20110813',
'20110817', '20110820', '20110824', '20110827', '20110831',
'20110903', '20110907', '20110910', '20110914','20110917', '20110921',
'20110924', '20110928', '20111001', '20111005', '20111008']

Is there a way to find all items between '20110809' and '20110911'?
So the subset would be:
['20110810','20110813', '20110817', '20110820', '20110824',
'20110827', '20110831', '20110903', '20110907', '20110910']

The keys are strings that represent a date in the format: 'YYYYMMDD'.

Thanks,

I have a list like so:

a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]

I would like to get a subset from the list with value between 10 and
20 (inclusive).

b = [10,13,15,14,20]

Is there a way to do this with a list or other data type?

Use a list comprehension:

b = [x for x in a if 10 <= x <= 20]

Cheers,
Ian
 
I

Ian Kelly

Depending on your historical programming-language baggage, "i" is usually
either an index or integer data, and since the source was a list of
integers, "i" didn't seem inappropriate.  Same for other common data-types:

 [f for f in (1.1, 2.2, 3.3) if 2.0 <= f < 3.0]
 [s for s in ("cat", "hat", "mat") if "bat" < s < "fat"]
 [c for c in "hello, world!" if 'a' <= c <= 'z']

"f" makes me think "function", not "float". As a general rule,
though, I prefer to name variables for what they represent, not for
their type.
 
T

Tim Chase

Can something like this be done with dictionarys?

For example, these are the keys in the dictionary from the call: dict.keys()

['20110601', '20110604', '20110608', '20110611', '20110615',
'20110618', '20110622', '20110625', '20110629', '20110702',
'20110706','20110709', '20110713', '20110716', '20110720', '20110723',
'20110727', '20110730', '20110803', '20110806', '20110810','20110813',
'20110817', '20110820', '20110824', '20110827', '20110831',
'20110903', '20110907', '20110910', '20110914','20110917', '20110921',
'20110924', '20110928', '20111001', '20111005', '20111008']

Is there a way to find all items between '20110809' and '20110911'?
So the subset would be:
['20110810','20110813', '20110817', '20110820', '20110824',
'20110827', '20110831', '20110903', '20110907', '20110910']

The keys are strings that represent a date in the format: 'YYYYMMDD'.

Since strings are comparable, you certainly can:

keys = [k for k in d.keys() if '20110809' < k < '20110911']

-tkc
 
I

Ian Kelly

Can something like this be done with dictionarys?

For example, these are the keys in the dictionary from the call: dict.keys()

['20110601', '20110604', '20110608', '20110611', '20110615',
'20110618', '20110622', '20110625', '20110629', '20110702',
'20110706','20110709', '20110713', '20110716', '20110720', '20110723',
'20110727', '20110730', '20110803', '20110806', '20110810','20110813',
'20110817', '20110820', '20110824', '20110827', '20110831',
'20110903', '20110907', '20110910', '20110914','20110917', '20110921',
'20110924', '20110928', '20111001', '20111005', '20111008']

Is there a way to find all items between '20110809' and '20110911'?
So the subset would be:
['20110810','20110813', '20110817', '20110820', '20110824',
'20110827', '20110831', '20110903', '20110907', '20110910']

Sure, dictionaries also support iteration.

date_range = [d for d in source_dict if '20110809' <= d <= '20110911']

Or if you want the result to also be a dictionary:

(Python 3)
date_range = {d:v for d, v in source_dict.items() if '20110809' <= d
<= '20110911'}

(Python 2)
date_range = dict((d,v) for d, v in source_dict.iteritems() if
'20110809' <= d <= '20110911')

You might also want to consider storing your dates as datetime.date
objects rather than strings, but since you already have them formatted
for lexicographical sorting it's not important for this.

Cheers,
Ian
 
C

Chris Angelico

At least in Python, there is no way that "and" could be a bitwise and
either, since it cannot be overloaded.

Like I said, cross-language safety-net. Sure it's not an issue here,
but when I write code in multiple languages, it's less embarrassing to
use an odd construct like that than to post code that outright doesn't
work. :)

ChrisA
 
W

Westley Martínez

Try a list comprehension:

a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]
[i for i in a if i>=10 if i<=20]

[10, 20, 15, 13, 14]

The double-if is new to me.  I thought it was an error when I first saw it,
but it seems to be legit syntax (or at least one that 2.7 tolerates,
intentionally or otherwise...).  I think I'd make it clearer with either

 [i for i in a if i>=10 and i<=20]

or even more clearly:

 [i for i in a if 10 <= i <= 20]

As long as we're nitpicking, I'll point out that "i" is an
inappropriate variable name here, since it is normally used to denote
indices, not data. That's why I used "x" in my response instead. ;-)

O that's what i stands for. I always thought it was integer o_O
 
S

Steven D'Aprano

Chris Angelico wrote:

Yeah, it's legal because you can nest fors and ifs in a list comp.
Stylistic difference, I used "if" instead of "and" because there's no
way that it could be a bitwise and.

If you're using Python, there's no way that it could be a bitwise and.

(It's a cross-language safety net that I sometimes use.)

I'm not familiar with a language that uses Python syntax but "and" is a
bitwise and. Which language is that?
 
C

Chris Angelico

I'm not familiar with a language that uses Python syntax but "and" is a
bitwise and. Which language is that?

I'm not familiar with any either, it's just that I have a few habits
that I slip into.

ChrisA
 
D

DevPlayer

(Python 3)
date_range = {d:v for d, v in source_dict.items() if '20110809' <= d
<= '20110911'}
Ian- Hide quoted text -
- Show quoted text -

(Python 2.7) supports dictionary comprehensions. I prehaps as early as
2.5.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,415
Latest member
SharonCran

Latest Threads

Top