简体   繁体   English

从python中的列表中删除一个位置到另一个位置元素

[英]Deleting one position to another position elements from list in python

I have a list like the one below and I would like to delete all entries between any word (inclusive) and the next '0' (exclusive).我有一个如下所示的列表,我想删除任何单词(包括)和下一个'0' (不包括)之间的所有条目。

So for example this list:所以例如这个列表:

array = ['1', '1', '0', '3', '0', '2', 'Continue', '1', '5', '1', '4', '0', '7', 'test', '3', '6', '0']

should become:应该变成:

['1', '1', '0', '3', '0', '2', '0', '7', '0']
array = ['1', '1', '0', '3', '0', '2', 'Continue', '1', '5', '1', '4', '0', '7', 'test', '3', '6', '0']

res = []
skip = False      #Flag to skip elements after a word
for i in array:
    if not skip:
        if i.isalpha():   #Check if element is alpha
            skip = True
            continue
        else:
            res.append(i)
    else:
        if i.isdigit():   #Check if element is digit
            if i == '0':
                res.append(i)
                skip = False

print res

Output:输出:

['1', '1', '0', '3', '0', '2', '0', '7', '0']

Kicking it old school -踢它老派-

array = ['1', '1', '0', '3', '0', '2', 'Continue', '1', '5', '1', '4', '0', '7', 'test', '3', '6', '0']
print(array)
array_op = []
i=0
while i < len(array):
    if not array[i].isdigit():
        i = array[i:].index('0')+i
        continue
    array_op.append(array[i])
    i += 1
print(array_op)

You can also do it by using exclusively list comprehension :您也可以通过专门使用list comprehension来做到这一点:

array = ['1', '1', '0', '3', '0', '2', 'Continue', '1', '5', '1', '4', '0', '7', 'test', '3', '6', '0']

# Find indices of strings in list
alphaIndex = [i for i in range(len(array)) if any(k.isalpha() for k in array[i])] 

# Find indices of first zero following each string
zeroIndex = [array.index('0',i) for i in alphaIndex] 

# Create a list with indices to be `blacklisted`
zippedIndex = [k for i,j in zip(alphaIndex, zeroIndex) for k in range(i,j)] 

# Filter the original list
array = [i for j,i in enumerate(array) if j not in zippedIndex] 

print(array)

Output:输出:

['1', '1', '0', '3', '0', '2', '0', '7', '0']

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

相关问题 更改列表中元素的 position 以便它们交叉匹配 Python 中的另一个列表 - Change position of elements in a list so they cross match another list in Python Python使用相同的len将列表的所有元素向后移动一个位置 - Python move all elements of a list one position back with same len Python:根据另一个列表中的元素删除列表元素 - Python: Deleting a list element based on elements from another list 从Python的另一个列表中获取项目列表中的位置 - Get position in list of items from another list in Python 如何在某个 position 将数字从列表更改为另一个? - How change numbers from a list to another one at a certain position? 如何在没有循环的情况下在同一索引 position 中用 python 中不同大小的另一个列表的元素替换列表的元素 - How to replace elements of a list with elements of another list of a different size in python in the same index position without a loop 检查项目在一个列表到另一个列表中的位置 - checking the position of an item in one list to another list 根据 Python 中另一个列表的位置选择列表的位置 - Pick position of a list based on the position of another list in Python Python中列表索引相对于另一个列表的位置 - Position of indices of a list with respect to another list in Python Python:更改实例列表中的元素位置 - Python: change elements position in a list of instances
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM