Reputation: 448
I have a large data set that produces a list after using the re.finditer function to find all instances of a certain character. 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. 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.
I am using python on a Mac OS 10.12.
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. Could I just do:
for idx, item in enumerate(a):
raw_text[item] = "new character/string"
Upvotes: 1
Views: 70
Reputation: 476544
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
. The 4
means that we take hops of 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
:
from itertools import islice
islice(data,3,None,4)
Here the None
is semantically used as the stop index. Since we don't want to stop at a certain index, we use None
. This will generate:
>>> list(islice(data,3,None,4))
[105, 777, 1449, 2121, 2793]
Upvotes: 8
Reputation: 5061
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]
Upvotes: 0
Reputation: 2791
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]
Upvotes: 1