Python列表切片

时间:2016-08-28 00:36:50

标签: 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 = []

slice是我需要从切片array获得的索引列表。 interval是用于在slice[i] is interval[j]时停止切片的索引列表,其中i和j是循环变量。 我需要根据array

时的条件,根据sliceintervals形成slice[i] is not interval[j]列表。
intermediate =intermediate + array[slice[i]:slice[i+1]+1]

在我的情况下:

slice[i]interval[j]相等于值12.所以我需要从array

形成一个列表列表
intermediate = array[slice[0]:slice[0+1]+1] + array[slice[2]:slice[2+1]+1] + array[slice[4]:slice[4+1]+1]

intermediate  = array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]  

以及slice[i] is interval[j] output = output + intermediate时切片继续。

output = output + [intermediate]

output = output + [array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]]

现在,间隔中的下一个值是17,所以直到我们在slice中有17个,我们从array[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]]

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

2 个答案:

答案 0 :(得分:3)

一个简单的解决方案:

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]]

我更改了一些变量名以避免冲突。 在大数据上,您可以将intervals转换为集合。

答案 1 :(得分:2)

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

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]]

数据

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]