如何在python中的两个值之间选择列表块

如何在python中的两个值之间选择列表块,python,list,select,Python,List,Select,我有一个这样的值列表: vect_i = ['a','X','c','g','X','t','o','X','q','w','e','r','t','y','u','i','o','p','Y','x','c','v','b','Y','b','n','m','Y','q','a','d','Y',] vect_f = ['q','w','e','r','t','y','u','i','o','p','Y','x','c','v','b'] 我的目标是以智能的方式仅选择最后一个X和第一个Y内

我有一个这样的值列表:

vect_i = ['a','X','c','g','X','t','o','X','q','w','e','r','t','y','u','i','o','p','Y','x','c','v','b','Y','b','n','m','Y','q','a','d','Y',]
vect_f = ['q','w','e','r','t','y','u','i','o','p','Y','x','c','v','b']
我的目标是以智能的方式仅选择最后一个X和第一个Y内的值(注意大写字母X和Y)

输出应如下所示:

vect_i = ['a','X','c','g','X','t','o','X','q','w','e','r','t','y','u','i','o','p','Y','x','c','v','b','Y','b','n','m','Y','q','a','d','Y',]
vect_f = ['q','w','e','r','t','y','u','i','o','p','Y','x','c','v','b']
有没有一个聪明的方法来做这个选择。我已经找到了一种方法,通过计算X和Y的数量,只收集相应索引之间所理解的内容,但我认为这是非常糟糕的编码

你能给我一个实现目标的好方法吗?

那就是我的方法:

indexOfLastX = len(vect_i) - 1 - vect_i[::-1].index("X")
indexOfFirstY = vect_i.index("Y")
print(vect_i[indexOfLastX + 1:indexOfFirstY])
结果是“['q','w','e','r','t','y','u','i','o','p']”。在您的示例中,结果是“Y”,当您想在第一个“Y”处停止时,令人惊讶的是…

这将是我的方式:

indexOfLastX = len(vect_i) - 1 - vect_i[::-1].index("X")
indexOfFirstY = vect_i.index("Y")
print(vect_i[indexOfLastX + 1:indexOfFirstY])
结果是“['q','w','e','r','t','y','u','i','o','p']”。在您的示例中,结果是一个“Y”,当您想在第一个“Y”处停止时,令人惊讶的是…

类似的情况

def sliceBetween(arr, ch1, ch2):
    if ch1 not in arr or ch2 not in arr:
        return []
    left = len(arr) - arr[::-1].index(ch1) - 1
    right = arr.index(ch2)
    if left >= right:
        return []
    return [left+1:right]
像这样的

def sliceBetween(arr, ch1, ch2):
    if ch1 not in arr or ch2 not in arr:
        return []
    left = len(arr) - arr[::-1].index(ch1) - 1
    right = arr.index(ch2)
    if left >= right:
        return []
    return [left+1:right]

请尝试使用以下代码:

endIndex = vect1.index('Y')
dum = ''.join(vect1)
startIndex = dum.rfind('X')
print vect1[startIndex+1:endIndex]

请尝试使用以下代码:

endIndex = vect1.index('Y')
dum = ''.join(vect1)
startIndex = dum.rfind('X')
print vect1[startIndex+1:endIndex]
重新获得所需的输出

将所需输出返回一行:

vect_i[(vect_i.reverse(), len(vect_i) - vect_i.index('X'), vect_i.reverse())[1] : vect_i.index('Y')]

一行:

vect_i[(vect_i.reverse(), len(vect_i) - vect_i.index('X'), vect_i.reverse())[1] : vect_i.index('Y')]


大多数其他答案要么多次调用
索引
,要么以某种形式修改和/或复制列表。我认为您可以简单地使用
枚举
并获得O(n)中所需位置的切片索引:


大多数其他答案要么多次调用
索引
,要么以某种形式修改和/或复制列表。我认为您可以简单地使用
枚举
并获得O(n)中所需位置的切片索引:


获取每个X>Y之间的所有差异,“最后一个X和第一个Y”是向量中的第一项

vect_f = []
x = [x for x,o in enumerate(reversed(vect_i)) if o == "X"]
for diff in x:
    sub_vect_f = []
    for sub_diff in range(len(vect_i)- diff, len(vect_i)):
        val = vect_i[sub_diff]
        if val!="Y":
            sub_vect_f.append(val)
        else:
            vect_f.append(sub_vect_f)
            break

for vect in vect_f:
    print (vect)

['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
['t', 'o', 'X', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
['c', 'g', 'X', 't', 'o', 'X', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']

获取每个X>Y之间的所有差异,“最后一个X和第一个Y”是向量中的第一项

vect_f = []
x = [x for x,o in enumerate(reversed(vect_i)) if o == "X"]
for diff in x:
    sub_vect_f = []
    for sub_diff in range(len(vect_i)- diff, len(vect_i)):
        val = vect_i[sub_diff]
        if val!="Y":
            sub_vect_f.append(val)
        else:
            vect_f.append(sub_vect_f)
            break

for vect in vect_f:
    print (vect)

['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
['t', 'o', 'X', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
['c', 'g', 'X', 't', 'o', 'X', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
在临时排序列表中搜索将非常快速(O(2个日志n))。但也有缺点-您需要使用原始索引创建额外的排序列表。如果需要使用不同的字符进行多次选择,并且原始列表不是很大,则可以使用它:

from bisect import bisect_left

sorted_list_with_idx = sorted((v,i) for i,v in enumerate(vect_i))

# -1 because we need to insert before index of first 'Y' in list
sorted_y_idx = bisect_left(sorted_list_with_idx, ('Y', -1))

# len(vect_i) used as max index to insert to right of last 'X'
# -1 because last 'X' will be before inserted 'new X'
sorted_x_idx = bisect_left(sorted_list_with_idx, ('X', len(vect_i))) - 1

y_idx = sorted_list_with_idx[sorted_y_idx][1]
x_idx = sorted_list_with_idx[sorted_x_idx][1]

>>> sorted_list_with_idx
[('X', 1), ('X', 4), ('X', 7), ('Y', 18), ('Y', 23), ('Y', 27), ('Y', 31),
 ('a', 0), ('a', 29), ('b', 22), ('b', 24), ('c', 2), ('c', 20), ('d', 30), 
 ('e', 10), ('g', 3), ('i', 15), ('m', 26), ('n', 25), ('o', 6), ('o', 16), 
 ('p', 17), ('q', 8), ('q', 28), ('r', 11), ('t', 5), ('t', 12), ('u', 14), 
 ('v', 21), ('w', 9), ('x', 19), ('y', 13)]

>>> y_idx
18
>>> x_idx
7

>>> vect_i[x_idx+1:y_idx]
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
在临时排序列表中搜索将非常快速(O(2个日志n))。但也有缺点-您需要使用原始索引创建额外的排序列表。如果需要使用不同的字符进行多次选择,并且原始列表不是很大,则可以使用它:

from bisect import bisect_left

sorted_list_with_idx = sorted((v,i) for i,v in enumerate(vect_i))

# -1 because we need to insert before index of first 'Y' in list
sorted_y_idx = bisect_left(sorted_list_with_idx, ('Y', -1))

# len(vect_i) used as max index to insert to right of last 'X'
# -1 because last 'X' will be before inserted 'new X'
sorted_x_idx = bisect_left(sorted_list_with_idx, ('X', len(vect_i))) - 1

y_idx = sorted_list_with_idx[sorted_y_idx][1]
x_idx = sorted_list_with_idx[sorted_x_idx][1]

>>> sorted_list_with_idx
[('X', 1), ('X', 4), ('X', 7), ('Y', 18), ('Y', 23), ('Y', 27), ('Y', 31),
 ('a', 0), ('a', 29), ('b', 22), ('b', 24), ('c', 2), ('c', 20), ('d', 30), 
 ('e', 10), ('g', 3), ('i', 15), ('m', 26), ('n', 25), ('o', 6), ('o', 16), 
 ('p', 17), ('q', 8), ('q', 28), ('r', 11), ('t', 5), ('t', 12), ('u', 14), 
 ('v', 21), ('w', 9), ('x', 19), ('y', 13)]

>>> y_idx
18
>>> x_idx
7

>>> vect_i[x_idx+1:y_idx]
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']

你能把你试过的东西的代码贴出来吗?听起来您只是想执行vect_f=vect_i[X:Y]list提供index()函数,返回列表中第一次出现的元素。要获取最后一个元素,只需还原列表。然后,您可以先对列表进行切片
Y
?为什么结果列表中有一个
Y
?@MosesKoledoye它是小写的“Y”。我猜这里区分大小写。@RohanAmrute请注意大写字母X和Y,您可以发布一些您尝试过的代码吗?听起来您只是想执行vect_f=vect_i[X:Y]list提供index()函数,返回列表中第一次出现的元素。要获取最后一个元素,只需还原列表。然后,您可以先对列表进行切片
Y
?为什么结果列表中有一个
Y
?@MosesKoledoye它是小写的“Y”。“我猜它在这里区分大小写。@RohanAmrute注意大写字母X和Y