繁体   English   中英

Python列表切片

[英]Python list slicing

我不明白该怎么办。 有人可以帮忙吗?

我有一些清单:

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是循环变量。 我需要基于sliceintervals的条件形成array列表列表,条件是当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)]]

现在interval中的下一个值是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]]

一个简单的解决方案:

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转换为一组。

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

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]

暂无
暂无

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

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