【问题标题】:Nested loop variables in PythonPython中的嵌套循环变量
【发布时间】:2018-06-11 02:22:34
【问题描述】:

我有一个数字列表; 1 6 3 15 54...

假设列表是 L,我想尽可能简单地应用这个条件;

L[2]-L[1] 
L[4]-L[3]
.
.
.

然后出现一个新列表,假设 L2,再次应用相同的规则;

L2[2]-L2[1]
L2[4]-L2[3]
.
.
.

直到从最初的L列表中只剩下一个数字。

我目前正在做类似的事情;

for n in range(2,len(L),1):
  r.append(s[n]-s[n-1])

适用于 1 个循环,但为了引入相应的迭代,我正在寻找一种体面的方法。我很乐意接受创新的答案。谢谢!

【问题讨论】:

  • 你能试着解释一下抽象一点吗?也许举几个例子?
  • 您应该自己编写代码,而不是让别人为您编写代码。我会“很乐意接受”在阳光下度过一个假期:-o
  • 在问题中,您将配对 2-1、4-3(可能还有 6-5、8-7),但您的示例还包括 3-2 和 5-4。哪个是正确的?
  • 这个过程是否被一遍又一遍地应用于同一个列表?
  • 我也不明白-7, -15, 27 是从哪里来的。应该是-8, 15, 27,不是吗?

标签: python


【解决方案1】:

假设您的示例应如下所示:

[1, 6, 3, 15, 54]
[5, -3, 12, 39]
[-8, 15, 27]
[23, 12]
[-11]

在每一步中,数字被视为一对,第一个数字从第二个数字中减去。

在这种情况下,您可以使用itertools recipes 中的pairwise 函数:

# original list
l = [1, 6, 3, 15, 54]

# copy it over to a new name which will be updated on each iteration
lst = l

# repeat until the list only contains a single element
while len(lst) > 1:
    # take all pairs from the list and substract the first from the second
    # collect the results in a list and assign that to `lst`
    lst = [y - x for x, y in pairwise(lst)]

# finally, print the single element that’s left in the list
print(lst[0])

【讨论】:

  • lst = [y - x for x, y in zip(lst,lst[1:])]
  • @splash58 lst[1:] 创建列表的新副本,pairwise 不会。
  • lst = [lst[x+1] - lst[x] for x in range(len(lst)-1)]
  • @splash58 这太不合 Python 了。
【解决方案2】:

假设最终[1, 6, 3, 15, 54] 被简化为[-11],您可以为此使用递归。递归在这里所做的就是一遍又一遍地调用相同的函数,直到满足基本情况。在您的情况下,它将不断减少每次递归调用的列表,直到列表的长度只有一个元素。我还假设您想在最后返回最终的单例列表。

递归方法:

lst = [1, 6, 3, 15, 54]

def reduce_list(l):
    if len(l) == 1:
        return l

    return reduce_list([y - x for x, y in zip(l, l[1:])])

print(reduce_list(lst))

哪些输出:

[-11]

注意: zip() 用于将列表中的每 2 个元素配对在一起,并从第一个元素中减去第二个元素。

这是一个分解上述zip()用法的示例:

>>> lst = [1, 6, 3, 15, 54]
>>> zipped = list(zip(lst, lst[1:]))
[(1, 6), (6, 3), (3, 15), (15, 54)]
>>> print([y - x for x, y in zipped])
[5, -3, 12, 39]

此外,使用递归完成的任何事情都可以通过一个简单的循环来完成,@poke 已经很好地展示了这一点。

编辑:

由于上述代码在大小为 1000 或更大的列表中容易出现RecursionError: maximum recursion depth 错误,因为 1000 是回收限制,您可以查看此post 了解有关如何增加限制的更多详细信息。或者,您也可以只制作一个绕过此问题的迭代版本,如下所示。

迭代方法:

lst = [1, 6, 3, 15, 54]

def reduce_list(l):
    return [y - x for x, y in zip(l, l[1:])]

# Since 5 elements reduce to 1, 4 iterations only needed
for _ in range(len(lst) - 1):
    lst = reduce_list(lst)

print(lst)
# [-11]

【讨论】:

  • RuntimeError: 超出最大递归深度有什么想法吗?
  • @y33t 你在什么例子上运行这个?
  • 我生成了一个包含大约 10 个元素的列表。
  • 是的,这很奇怪,我在 100 个元素上运行此代码并且工作正常。您可以使用@poke 的示例,与此示例相比,它更简单且容易出错。
  • @y33t 在底部做了一个迭代版本。
【解决方案3】:

您可以尝试递归方法:

data=[1,6,3,15,54]

def finding(list_1):
    if not list_1:
        return 0
    list_2=[]
    for i in range(0,len(list_1),1):
        chunk=list_1[i:i+2]
        if len(chunk)==2:
            list_2.append(chunk[1]-chunk[0])

    if list_2:
        print(list_2)



    return finding(list_2)


print(finding(data))

输出:

[5, -3, 12, 39]
[-8, 15, 27]
[23, 12]
[-11]
0

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2018-12-25
    相关资源
    最近更新 更多