Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取包含特定项的所有可能的列表切片_Python_Python 3.x_List_Slice - Fatal编程技术网

Python 获取包含特定项的所有可能的列表切片

Python 获取包含特定项的所有可能的列表切片,python,python-3.x,list,slice,Python,Python 3.x,List,Slice,我正在尝试查找包含特定项的所有列表片段。 假设我有一个由五个语素组成的列表,其中一个是词干,我想找到包含它的每个可能片段。以下是我为此编写的代码: stem = 'stm' w = ['a', 'b', stem, 'c', 'd'] w2 = w stem_index = w.index(stem) stem_slice1 = w[stem_index:] stem_slice2 = w[:stem_index + 1] slices = [] while len(w) > 0:

我正在尝试查找包含特定项的所有列表片段。 假设我有一个由五个语素组成的列表,其中一个是词干,我想找到包含它的每个可能片段。以下是我为此编写的代码:

stem = 'stm'
w = ['a', 'b', stem, 'c', 'd']
w2 = w
stem_index = w.index(stem)
stem_slice1 = w[stem_index:]
stem_slice2 = w[:stem_index + 1]
slices = []

while len(w) > 0:
    w = w[:-1] # chops the last item
    if stem in w and w not in slices:
        slices.append(w)

    w_ = w[1:] # then chops the first item
    if stem in w_ and w_ not in slices:
        slices.append(w_)

    w2 = w2[1:] # chops the first item
    if stem in w2 and w2 not in slices:
        slices.append(w2)

    w2_ = w2[:-1] # then chops the last item
    if stem in w2_ and w2_ not in slices:
        slices.append(w2_)

while len(stem_slice1) > 0:
    stem_slice1 = stem_slice1[:-1]
    if stem in stem_slice1 and stem_slice1 not in slices:
        slices.append(stem_slice1)

while len(stem_slice2) > 0:
    stem_slice2 = stem_slice2[1:]
    if stem in stem_slice2 and stem_slice2 not in slices:
        slices.append(stem_slice2)

print (slices)
运行时,此代码将打印:

[['a', 'b', 'stm', 'c'], ['b', 'stm', 'c'], ['b', 'stm', 'c', 'd'], ['a', 'b', 'stm'], ['b', 'stm'], ['stm', 'c', 'd'], ['stm', 'c'], ['stm']]

它似乎工作得很好,但我想知道是否有一种更像python的方法来做同样的事情。

只要得到所有有效的开始和结束索引的笛卡尔乘积就可以了。换句话说,两个
for
循环就足够了

stem = 'stm'
w = ['a', 'b', stem, 'c', 'd']
idx = w.index(stem)
slices = []
for start in range(idx+1):
    for end in range(idx+1, len(w)+1):
        slices.append(w[start:end])
print(slices)
结果:

[['a', 'b', 'stm'], ['a', 'b', 'stm', 'c'], ['a', 'b', 'stm', 'c', 'd'], ['b', 'stm'], ['b', 'stm', 'c'], ['b', 'stm', 'c', 'd'], ['stm'], ['stm', 'c'], ['stm', 'c', 'd']]

只要得到所有有效开始和结束指数的笛卡尔积就可以了。换句话说,两个
for
循环就足够了

stem = 'stm'
w = ['a', 'b', stem, 'c', 'd']
idx = w.index(stem)
slices = []
for start in range(idx+1):
    for end in range(idx+1, len(w)+1):
        slices.append(w[start:end])
print(slices)
结果:

[['a', 'b', 'stm'], ['a', 'b', 'stm', 'c'], ['a', 'b', 'stm', 'c', 'd'], ['b', 'stm'], ['b', 'stm', 'c'], ['b', 'stm', 'c', 'd'], ['stm'], ['stm', 'c'], ['stm', 'c', 'd']]

是否有多个stem实例,其中任何一个都意味着您需要子切片?我的工作假设stem已被指定为stem,因此将只有一个stem。将切片作为一个集合,然后去掉所有的“not in”子句。集合不接受列表。我会得到错误:unhabable类型:'list'。是否有多个stem实例,其中任何一个都意味着您需要子切片?我的工作假设stem已经被指定为stem,因此将只有一个stem。将切片作为一个集合,然后去掉所有的“not in”子句。集合不接受列表。我会得到错误:unhabable类型:“list”。谢谢你,凯文。那当然更经济;)或者,作为一个列表理解:slices=[w[start:end]表示范围内的开始(idx+1)表示范围内的结束(idx+1,len(w)+1)]谢谢Kevin。那当然更经济;)或者,作为列表理解:slices=[w[start:end]表示范围内的开始(idx+1)表示范围内的结束(idx+1,len(w)+1)]