简体   繁体   English

从列表中删除特定项目,但保留一些在python中

[英]Removing specific items from a list but keeping some in python

I have a large data set that produces a list after using the re.finditer function to find all instances of a certain character. 我有一个很大的数据集,使用re.finditer函数查找某个字符的所有实例后,会产生一个列表。 Sample list shown here: 示例列表如下所示:

[41, 64, 87, 105, 713, 736, 759, 777, 1385, 1408, 1431, 1449, 2057, 2080, 2103, 2121, 2729, 2752, 2775, 2793,...]

I need to delete all but every 4th item. 我需要删除所有(但第4个项目除外)。 So I need to delete the character at index 41, 64, and 87 but not 105. Delete 713, 736, and 759 but not 777. etc. 因此,我需要删除索引41、64和87处的字符,但不删除105。删除713、736和759,但不删除777。等等。

I am using python on a Mac OS 10.12. 我在Mac OS 10.12上使用python。

UPDATE: 更新:

So now I have this new list a=[105,777,1449,2121,2793] and I wish to replace the indices of a textfile which I have imported into a variable. 因此,现在我有了这个新列表a=[105,777,1449,2121,2793]并且希望替换已导入到变量中的文本文件的索引。 Could I just do: 我可以做:

for idx, item in enumerate(a):
     raw_text[item] = "new character/string"

Based on your description, you want to remove all but every fourth element. 根据您的描述,您希望删除除第四个元素以外的所有元素。 You can do this with a slicing operator: 您可以使用切片运算符执行此操作:

data[3::4]

Here 3 is the start index since the first index we are interested in, is at 3 . 这里3起始索引,因为我们感兴趣的第一个索引是3 The 4 means that we take hops of 4 . 4表示我们跳4

This generates: 这将产生:

>>> data[3::4]
[105, 777, 1449, 2121, 2793]

In case you are working with an iterable (not a list, tuple,...), you can use itertools.islice : 如果您使用的是可迭代对象 (而不是列表,元组等),则可以使用itertools.islice

from itertools import islice

islice(data,3,None,4)

Here the None is semantically used as the stop index . 在这里, None在语义上用作停止索引 Since we don't want to stop at a certain index, we use None . 由于我们不想停在某个索引处,因此我们使用None This will generate: 这将生成:

>>> list(islice(data,3,None,4))
[105, 777, 1449, 2121, 2793]
a = [41, 64, 87, 105, 713, 736, 759,
     777, 1385, 1408, 1431, 1449, 2057, 
     2080, 2103, 2121, 2729, 2752, 2775, 2793]

b = a[3::4]
print(b)

# Output:
# [105, 777, 1449, 2121, 2793]

Its little different but if you start reverse indexing then it ll give you same 差别不大,但是如果您开始反向索引,它将为您提供相同的

In [29]: s = d[::-4]

In [30]: s
Out[30]: [2793, 2121, 1449, 777, 105]

In [31]: s.reverse()

In [32]: s
Out[32]: [105, 777, 1449, 2121, 2793]

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

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