跳过python中范围函数中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24089924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:57:56  来源:igfitidea点击:

Skip over a value in the range function in python

pythonloopsfor-looprange

提问by David

What is the pythonic way of looping through a range of numbers and skipping over one value? For example, the range is from 0 to 100 and I would like to skip 50.

循环遍历一系列数字并跳过一个值的pythonic方法是什么?例如,范围是从 0 到 100,我想跳过 50。

Edit: Here's the code that I'm using

编辑:这是我正在使用的代码

for i in range(0, len(list)):
    x= listRow(list, i)
    for j in range (#0 to len(list) not including x#)
        ...

采纳答案by njzk2

You can use any of these:

您可以使用以下任何一种:

# Create a range that does not contain 50
for i in [x for x in xrange(100) if x != 50]:
    print i

# Create 2 ranges [0,49] and [51, 100] (Python 2)
for i in range(50) + range(51, 100):
    print i

# Create a iterator and skip 50
xr = iter(xrange(100))
for i in xr:
    print i
    if i == 49:
        next(xr)

# Simply continue in the loop if the number is 50
for i in range(100):
    if i == 50:
        continue
    print i

回答by Daniel

for i in range(100):
    if i == 50:
        continue
    dosomething

回答by MxLDevs

It depends on what you want to do. For example you could stick in some conditionals like this in your comprehensions:

这取决于你想做什么。例如,你可以在你的理解中坚持一些这样的条件:

# get the squares of each number from 1 to 9, excluding 2
myList = [i**2 for i in range(10) if i != 2]
print(myList)

# --> [0, 1, 9, 16, 25, 36, 49, 64, 81]

回答by Anonemuss

what you could do, is put an if statement around everything inside the loop that you want kept away from the 50. e.g.

您可以做的是在循环内的所有内容周围放置一个 if 语句,您希望远离 50。例如

for i in range(0, len(list)):
    if i != 50:
        x= listRow(list, i)
        for j in range (#0 to len(list) not including x#)

回答by Smittie

In addition to the Python 2 approach here are the equivalents for Python 3:

除了 Python 2 方法之外,这里还有 Python 3 的等效方法:

# Create a range that does not contain 50
for i in [x for x in range(100) if x != 50]:
    print(i)

# Create 2 ranges [0,49] and [51, 100]
from itertools import chain
concatenated = chain(range(50), range(51, 100))
for i in concatenated:
    print(i)

# Create a iterator and skip 50
xr = iter(range(100))
for i in xr:
    print(i)
    if i == 49:
        next(xr)

# Simply continue in the loop if the number is 50
for i in range(100):
    if i == 50:
        continue
    print(i)

Ranges are lists in Python 2 and iterators in Python 3.

范围是 Python 2 中的列表和 Python 3 中的迭代器。

回答by Acumenus

It is time inefficient to compare each number, needlessly leading to a linear complexity. Having said that, this approach avoids any inequality checks:

比较每个数字的时间效率低下,不必要地导致线性复杂性。话虽如此,这种方法避免了任何不等式检查:

import itertools

m, n = 5, 10
for i in itertools.chain(range(m), range(m + 1, n)):
    print(i)  # skips m = 5

As an aside, you woudn't want to use (*range(m), *range(m + 1, n))even though it works because it will expand the iterables into a tuple and this is memory inefficient.

顺便说一句,(*range(m), *range(m + 1, n))即使它可以工作,您也不想使用,因为它会将可迭代对象扩展为一个元组,而这是内存效率低下的。



Credit: comment by njzk2, answer by Locke

信用:njzk2 的评论,Locke 的回答

回答by Maria

for i in range(0, 101):
if i != 50:
    do sth
else:
    pass