在两个不同区间之间的for循环

huangapple go评论75阅读模式
英文:

For loop between two different intervals

问题

这是一个百分之百愚蠢的问题,但我找不到答案。我能够在两个不同的间隔之间执行for循环吗?

以一个例子来说明:

array = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,5) and range(7,9):
    print(array[i])

它会输出:
1,2,3,4,5,8,9

编辑:
正如Adam.Er8指出的,
itertools.chain 将是更干净的解决方案。

在我进行实验时发现的另一种解决方案是没有使用itertools的情况下:

array = [1,2,3,4,5,6,7,8,9,10]
needrange = [[1,3],[5,7],[8,9]]
for j in range(0,len(needrange)):
    for i in range(needrange[j][0],needrange[j][1]):
        print(array[i])

这个解决方案不够简洁,但如果你不想导入模块的话,它可以完成任务。

感谢来自类似问题的cs95的贡献,他写道:

ranges = [range(2, 5), range(3, 7), ...]
flat = list(chain.from_iterable(ranges))

在撰写这个回答时,我注意到:

我相当肯定cs95的答案中的range(n,m)也会返回一个列表,就像我一开始创建的列表一样。因此非常相似。

itertools 中的函数,在逻辑上看起来非常相似,但我发现我的解决方案更容易阅读。

英文:

This is 100% a dumb question but I couldn't find the answer. Would i be able to do a for loop between two different intervals?

Take for example:

array = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,5) and range(7,9):
    print(array[i])

and it would output:
1,2,3,4,5,8,9

Edit:
As Adam.Er8 pointed out,
itertools.chain, would be the cleaner solutions.

Another solution I found while experimenting was without the itertools would be:

array = [1,2,3,4,5,6,7,8,9,10]
needrange = [[1,3],[5,7],[8,9]]
for j in range(0,len(needrange)):
    for i in range(needrange[j][0],needrange[j][1]):
        print(array[i])

This solution isn't clean but it does do the job if you aren't looking to import.

Credit to cs95 from a similar question, who wrote:

ranges = [range(2, 5), range(3, 7), ...]
flat = list(chain.from_iterable(ranges))

Notes found while writing this response):

I'm pretty sure range(n,m) from cs95 answer just returns a list as well, whereas I just created a list initially. So very similar

The function from itertools,looks very similar logic wise, but I just found my solution easier to read.

答案1

得分: 3

你可以使用itertools.chain(range(0,5), range(7,9))

英文:

You can use itertools.chain(range(0,5), range(7,9))

答案2

得分: 1

不太漂亮,但你可以这样做

for i in list(range(0,5)) + list(range(7,9)):
print(array[i])


你可以将列表相加;只是不能将范围相加。

嗯,这并不完全正确,可以使用 [`itertools.chain`](https://docs.python.org/3/library/itertools.html#itertools.chain):

from itertools import chain
for i in chain(range(0,5), range(7,9)):
print(array[i])

它实际上并没有将它们相加在一起;它只是"链"了任何可迭代对象,包括一个范围对象。
英文:

Not pretty, but you can do

for i in list(range(0,5)) + list(range(7,9)):
    print(array[i])

You can add lists together; just not ranges.

Well, that's not entirely true, using itertools.chain:

from itertools import chain
for i in chain(range(0,5), range(7,9)):
    print(array[i])

It doesn't really add them together; it just "chains" any iterable, including a range object.

答案3

得分: 1

array = [1,2,3,4,5,6,7,8,9,10]
for i in [*range(0,5), *range(7,9)]:
print(array[i])

英文:
array = [1,2,3,4,5,6,7,8,9,10]
for i in [*range(0,5), *range(7,9)]:
    print(array[i])

答案4

得分: 0

我假设你的意思是“如何在两个不同的范围之间创建一个联合?”,在这种情况下:

for i in list(range(0, 5)) + list(range(7, 9)):
    # ...

不过,需要警告一下:用这种方式会将范围扩展为一个列表,将其中的每个元素都放在自己的条目中。对于大范围的操作可能会导致内存问题。

英文:

I'm assuming you mean "how do I create an union between 2 different ranges?", in which case:

for i in list(range(0, 5)) + list(range(7, 9)):
    # ...

Although, a warning: Doing it this way will expand the range into a list, putting every element in it in its own entry. Doing this with a large range may cause memory issues

huangapple
  • 本文由 发表于 2023年6月22日 15:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定