简体   繁体   English

在python中获得子列表的更好方法

[英]Better way to get sublist in python

I am working on the following problem: This function returns a list of all possible sublists in L of length n without skipping elements in L. The sublists in the returned list should be ordered in the way they appear in L, with those sublists starting from a smaller index being at the front of the list. 我正在解决以下问题:此函数返回L长度为n的所有可能子列表的列表,而不跳过L中的元素。返回列表中的子列表应以它们在L中出现的方式排序,这些子列表从较小的索引位于列表的前面。

Example 1, if L = [10, 4, 6, 8, 3, 4, 5, 7, 7, 2] and n = 4 then your function should return the list [[10, 4, 6, 8], [4, 6, 8, 3], [6, 8, 3, 4], [8, 3, 4, 5], [3, 4, 5, 7], [4, 5, 7, 7], [5, 7, 7, 2]] Example 1, if L = [10, 4, 6, 8, 3, 4, 5, 7, 7, 2] and n = 4则您的函数应返回列表[[10, 4, 6, 8], [4, 6, 8, 3], [6, 8, 3, 4], [8, 3, 4, 5], [3, 4, 5, 7], [4, 5, 7, 7], [5, 7, 7, 2]]

My solution works but how can I make it shorter? 我的解决方案有效,但是如何使它更短? What is a better way to do this? 有什么更好的方法可以做到这一点?

def getSublists(L, n):

    newN = n
    myList = []
    for i in range(len(L)):
        orginalLen = L[i:n]
        if(len(orginalLen) == n):
            myList.append(L[i:n])
            n = n + 1
        else:
            myList.append(L[i:n])
            n = n + 1
    if(newN == 1):
        print(myList)
    else:
        print(myList[:len(myList)-(n-1)])
getSublists([10, 4, 6, 8, 3, 4, 5, 7, 7, 2],4)

getSublists([1], 1)

getSublists([0, 0, 0, 0, 0], 2)



OUTPUT
[[10, 4, 6, 8], [4, 6, 8, 3], [6, 8, 3, 4], [8, 3, 4, 5], [3, 4, 5, 7], [4, 5, 7, 7], [5, 7, 7, 2]]
[[1]]
[[0, 0], [0, 0], [0, 0], [0, 0]]
l = [1,2,3,4,5,6,87,9]
n = ..
print [l[i:i+n] for i in range(len(l)-n+1)]

maybe you need. 也许你需要。

In one line: 一行:

get_sublists = lambda ls, n: [ls[x:x+n] for x in range(len(ls)-n+1)]


get_sublists([10, 4, 6, 8, 3, 4, 5, 7, 7, 2], 4)

[[10, 4, 6, 8], [4, 6, 8, 3], [6, 8, 3, 4], [8, 3, 4, 5], [3, 4, 5, 7], [4, 5, 7, 7], [5, 7, 7, 2]] [[10,4,6,8],[4,6,8,3],[6,8,3,4],[8,3,4,5],[3,4,5,7] ,[4、5、7、7],[5、7、7、2]]

def get_sublists(L, n):
    return [ L[i:i+n] for i in range(len(L)-n) ]

I completed the program a little better understanding of the reader. 我完成了该程序,使读者有了更好的理解。

def getSublists(L, n):
    new_list = []
    for i in range(len(L)-n+1):
           a = L[i:i+n]
           new_list.append(a)
    return new_list 

answer: 回答:

[[10, 4, 6, 8],
 [4, 6, 8, 3],
 [6, 8, 3, 4],
 [8, 3, 4, 5],
 [3, 4, 5, 7],
 [4, 5, 7, 7],
 [5, 7, 7, 2]]

This is pretty readable I think, to understand the concept. 我认为这很容易理解。 The idea here is to iterate through the numbers from 0 to the length of L, minus 4. And just take the sublist of L from your current index i, to i+4. 这里的想法是迭代从0到L的长度减去4的数字,然后将L的子列表从当前索引i变为i + 4。 Iterating to length-4 ensures you don't try to access an index out of bounds! 迭代到length-4可以确保您不会尝试超出范围的索引!

>>> for i in range(len(L)-4+1):
           print L[i:i+4]

    [10, 4, 6, 8]
    [4, 6, 8, 3]
    [6, 8, 3, 4]
    [8, 3, 4, 5]
    [3, 4, 5, 7]
    [4, 5, 7, 7]
    [5, 7, 7, 2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM