Python列表切片

Python列表切片,python,list,Python,List,我不知道在这里该做什么。有人能帮忙吗 我有几个清单: array = [7,8,2,3,4,10,5,6,7,10,8,9,10,4,5,12,13,14,1,2,15,16,17] slice = [2, 4, 6, 8, 10, 12, 15, 17, 20, 22] intervals = [12, 17, 22] output = [] intermediate = [] 切片是我需要从切片数组中获取的索引列表。interval是一个索引列表,用于在slice[i]为interval[

我不知道在这里该做什么。有人能帮忙吗

我有几个清单:

array = [7,8,2,3,4,10,5,6,7,10,8,9,10,4,5,12,13,14,1,2,15,16,17]
slice = [2, 4, 6, 8, 10, 12, 15, 17, 20, 22]
intervals = [12, 17, 22]
output = []
intermediate = []
切片是我需要从切片数组中获取的索引列表。interval是一个索引列表,用于在slice[i]为interval[j]时停止切片,其中i和j是循环变量。 我需要根据slice和interval从数组中形成列表列表,条件是slice[I]不是interval[j]

就我而言:

当值12的切片[i]和间隔[j]相等时。所以我需要从数组中形成一个列表

那是

intermediate  = array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]  
output = output + [array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]]
output = [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]
当切片[i]是间隔[j]时,输出=输出+中间,切片继续

output = output + [intermediate]
那是

intermediate  = array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]  
output = output + [array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]]
output = [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]
现在区间中的下一个值是17,所以在切片中有17个之前,我们从数组[slice[6]:slice[6+1]+1]中形成另一个列表,并将其添加到输出中。这种情况还在继续

最终输出应为:

output = [array[slice[0]:slice[0+1]+1] + array[slice[2]:slice[2+1]+1] + array[slice[4]:slice[4+1]+1] , array[slice[6]:slice[6+1]+1], array[slice[8]:slice[8+1]+1]]
那是

intermediate  = array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]  
output = output + [array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]]
output = [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]

一个简单的解决方案:

array_ = [7,8,2,3,4,10,5,6,7,10,8,9,10,4,5,12,13,14,1,2,15,16,17]
slice_ = [2, 4, 6, 8, 10, 12, 15, 17, 20, 22]
intervals = [12, 17, 22]
output = []
intermediate = []

for i in range(0, len(slice_), 2):
    intermediate.extend(array_[slice_[i]:slice_[i+1]+1])
    if slice_[i+1] in intervals:
        output.append(intermediate)
        intermediate = []

print output
# [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]
我更改了一些变量名以避免冲突。
对于大数据,您可以将间隔转换为一个集合。

一个简单的解决方案:

array_ = [7,8,2,3,4,10,5,6,7,10,8,9,10,4,5,12,13,14,1,2,15,16,17]
slice_ = [2, 4, 6, 8, 10, 12, 15, 17, 20, 22]
intervals = [12, 17, 22]
output = []
intermediate = []

for i in range(0, len(slice_), 2):
    intermediate.extend(array_[slice_[i]:slice_[i+1]+1])
    if slice_[i+1] in intervals:
        output.append(intermediate)
        intermediate = []

print output
# [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]
我更改了一些变量名以避免冲突。
在大数据上,您可以将间隔转换为一个集合。

这里有一个递归解决方案,它将遍历索引一次,并动态检查索引是否在间隔内,并相应地将切片结果附加到列表中:

def slicing(array, index, stops, sliced):
    # if the length of index is smaller than two, stop
    if len(index) < 2:
        return 

    # if the first element of the index in the intervals, create a new list in the result 
    # accordingly and move one index forward
    elif index[0] in stops:
        if len(index) >= 3:
            sliced += [[]]
            slicing(array, index[1:], stops, sliced)

    # if the second element of the index is in the intervals, append the slice to the last
    # element of the list, create a new sublist and move two indexes forward accordingly
    elif index[1] in stops:
        sliced[-1] += array[index[0]:(index[1]+1)]
        if len(index) >= 4:
            sliced += [[]]
            slicing(array, index[2:], stops, sliced)

    # append the new slice to the last element of the result list and move two index 
    # forward if none of the above conditions satisfied:       
    else:
        sliced[-1] += array[index[0]:(index[1]+1)]
        slicing(array, index[2:], stops, sliced)

sliced = [[]]
slicing(array, slice_, intervals, sliced)

sliced
# [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]

下面是一个递归解决方案,它将遍历索引一次,并动态检查索引是否在间隔内,并相应地将切片结果附加到列表中:

def slicing(array, index, stops, sliced):
    # if the length of index is smaller than two, stop
    if len(index) < 2:
        return 

    # if the first element of the index in the intervals, create a new list in the result 
    # accordingly and move one index forward
    elif index[0] in stops:
        if len(index) >= 3:
            sliced += [[]]
            slicing(array, index[1:], stops, sliced)

    # if the second element of the index is in the intervals, append the slice to the last
    # element of the list, create a new sublist and move two indexes forward accordingly
    elif index[1] in stops:
        sliced[-1] += array[index[0]:(index[1]+1)]
        if len(index) >= 4:
            sliced += [[]]
            slicing(array, index[2:], stops, sliced)

    # append the new slice to the last element of the result list and move two index 
    # forward if none of the above conditions satisfied:       
    else:
        sliced[-1] += array[index[0]:(index[1]+1)]
        slicing(array, index[2:], stops, sliced)

sliced = [[]]
slicing(array, slice_, intervals, sliced)

sliced
# [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]

如果切片包含两个间隔之间的奇数元素怎么办?由于最后一个索引是成对切片,是否应该丢弃它?切片严格来说是偶数。切片是一个。不要将其用作变量名。@RickTeachey slice只是一个内置类的名称,但我强烈同意不使用它和其他内置名称。导入关键字;printkeyword.kwlist列出保留的关键字。进口内置设备;PrintDirBuiltinsPy3列出了所有的内置名称。嘿,伙计们,我会记住的。感谢您的输入。如果切片包含两个间隔之间的奇数元素,该怎么办?由于最后一个索引是成对切片,是否应该丢弃它?切片严格来说是偶数。切片是一个。不要将其用作变量名。@RickTeachey slice只是一个内置类的名称,但我强烈同意不使用它和其他内置名称。导入关键字;printkeyword.kwlist列出保留的关键字。进口内置设备;PrintDirBuiltinsPy3列出了所有的内置名称。嘿,伙计们,我会记住的。谢谢你的意见。