如何在Python中设置范围函数的限制?

发布于 2025-01-31 13:48:06 字数 318 浏览 2 评论 0原文

说我在列表中迭代。我想检查列表的邻居(I-1和I+1)是否包含某个元素。我该如何执行此操作而不遇到“列表索引超出范围”问题? 示例:

list = [1, 0, 1, 0, 1, 0]
for i, j in enumerate(list):
    elements = 0
    for m in range(i-1,i+2):
        if list[m] == 1:
            elements += 1
    print(list[i], elements)

如何为范围函数设置边界,以免它以低于0及以上的LEN(列表)?

Say I am iterating through a list. I want to check if the list's neighbours (i-1 and i+1) contain a certain element. How do I do this without running into "list index out of range" problem?
Example:

list = [1, 0, 1, 0, 1, 0]
for i, j in enumerate(list):
    elements = 0
    for m in range(i-1,i+2):
        if list[m] == 1:
            elements += 1
    print(list[i], elements)

How do I set boundaries for the range function, so that it doesn't go below 0 and above len(list)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耶耶耶 2025-02-07 13:48:06

如果要迭代目标列表中的所有元素,一个解决方案是检查循环的第二个值:

_list = [1, 0, 1, 0, 1, 0]
elements = 0
for i, j in enumerate(_list):
    for m in range(max(i-1, 0), min(i+2, len(_list))):
        if _list[m] == 1:
            elements += 1

If you want to iterate for all elements in the target list, one solution is to check the value of second for loop:

_list = [1, 0, 1, 0, 1, 0]
elements = 0
for i, j in enumerate(_list):
    for m in range(max(i-1, 0), min(i+2, len(_list))):
        if _list[m] == 1:
            elements += 1
椒妓 2025-02-07 13:48:06

尝试从顶部和底部切片列表

list = [1, 0, 1, 0, 1, 0]
elements = 0
# slice list and start from second element and finish at the penultimate element
for i, j in enumerate(list[1:-1], 1):
    for m in range(i-1,i+2):
        if list[m] == 1:
            elements += 1

,或者因为您不使用外循环中的列表项,请在范围内循环

elements = 0
# start from the second index and finish at the penultimate index
for i in range(1, len(list)-1):
    for m in range(i-1,i+2):
        if list[m] == 1:
            elements += 1

Try slicing list from the top and bottom

list = [1, 0, 1, 0, 1, 0]
elements = 0
# slice list and start from second element and finish at the penultimate element
for i, j in enumerate(list[1:-1], 1):
    for m in range(i-1,i+2):
        if list[m] == 1:
            elements += 1

or since you don't use list items in the outer loop, loop over range

elements = 0
# start from the second index and finish at the penultimate index
for i in range(1, len(list)-1):
    for m in range(i-1,i+2):
        if list[m] == 1:
            elements += 1
柒七 2025-02-07 13:48:06

听起来您想使用窗口函数。这些年来,我在这里的某个地方一直在使用它:

from typing import Generator
from itertools import islice

def window(seq, n: int = 2) -> Generator:
    """
    Returns a sliding window (of width n) over data from the iterable
    """
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result
    for elem in it:
        result = result[1:] + (elem,)
        yield result
        

mylist = [1, 0, 1, 0, 1, 0, 5]
for chunk in window(mylist, n=3):
    print(chunk)

这将为您提供:

(1,0,1)
(0,1,0)
(1,0,1)
(0,1,0)
(1,0,5)

您可以在其中比较结果的“窗口”的内容。

Sounds like you want to use a window function. I got this somewhere here and have been using it over the years:

from typing import Generator
from itertools import islice

def window(seq, n: int = 2) -> Generator:
    """
    Returns a sliding window (of width n) over data from the iterable
    """
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result
    for elem in it:
        result = result[1:] + (elem,)
        yield result
        

mylist = [1, 0, 1, 0, 1, 0, 5]
for chunk in window(mylist, n=3):
    print(chunk)

This will give you :

(1, 0, 1)
(0, 1, 0)
(1, 0, 1)
(0, 1, 0)
(1, 0, 5)

Where you can compare the contents of the resulting 'window' however you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文