473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

a quickie: range - x

I want an iterable from 0 to N except for element m (<=M).
I could write
x = range(N)
x.remove(m)
but I want it in one expression.

Thanks,
Ross

Nov 30 '06 #1
5 1114
(x for x in xrange(N+1) if x != m)

rjtucke wrote:
I want an iterable from 0 to N except for element m (<=M).
I could write
x = range(N)
x.remove(m)
but I want it in one expression.

Thanks,
Ross
Nov 30 '06 #2
On Wed, 29 Nov 2006 19:42:16 -0800, rjtucke wrote:
I want an iterable from 0 to N except for element m (<=M).
I could write
x = range(N)
x.remove(m)
but I want it in one expression.
Because the world will end if you can't?

What's wrong with the solution you already have? If you need to use it
many times, make it a function. Calling the function is always a one-liner.

Here's another solution:

x = [i for i in range(N) if i != m]

Here's another:

x = range(m-1) + range(m+1, N)

Here's a generator to do it:

def range_except(N, m):
"""Iterator that returns ints from 0 through N-1 except for m."""
for i in xrange(N):
if i != m:
yield i


--
Steven.

Nov 30 '06 #3
"rjtucke" <rj*****@gmail.comwrites:
I want an iterable from 0 to N except for element m (<=M).
I could write
x = range(N)
x.remove(m)
but I want it in one expression.
itertools.chain(xrange(0,m), xrange(m+1, N))
Nov 30 '06 #4
Thanks, all- that helped.
Ross

Nov 30 '06 #5
Steven D'Aprano wrote:
On Wed, 29 Nov 2006 19:42:16 -0800, rjtucke wrote:
>I want an iterable from 0 to N except for element m (<=M).
x = range(m-1) + range(m+1, N)
Should be range(m) + range(m+1, N)

Kent
Nov 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hey all. Anyone who is familiar with Python programming knows that you can have code like this: list = This code puts all the items processed by the for loop in a list plus 1. Is there a way...
7
by: Robin Haswell | last post by:
Hey guys. This should just be a quickie: I can't figure out how to convert r"\x2019" to an int - could someone give me a hand please? Cheers -Rob
85
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the...
0
by: iain654 | last post by:
I have finally worked out how to automatically send a range of cells in the body of an email using Outlook but it is very clumsy and I have to build up the email using the limit of line...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.