Python - 计算指定值范围内的列表元素

use*_*059 2 python

我有很多单词:

my_list = ['[tag]', 'there', 'are', 'many', 'words', 'here', '[/tag]', '[tag]', 'some', 'more', 'here', '[/tag]', '[tag]', 'and', 'more', '[/tag]']
Run Code Online (Sandbox Code Playgroud)

我希望能够计算整个列表中[tag]元素之间(和包括)元素的数量.目标是能够看到频率分布.

我可以range()用来开始和停止字符串匹配吗?

Hoo*_*ing 5

首先,找到所有索引[tag],相邻索引之间的差异是单词的数量.

my_list = ['[tag]', 'there', 'are', 'many', 'words', 'here', '[/tag]', '[tag]', 'some', 'more', 'here', '[/tag]', '[tag]', 'and', 'more', '[/tag]']
indices = [i for i, x in enumerate(my_list) if x == "[tag]"]
nums = []
for i in range(1,len(indices)):
    nums.append(indices[i] - indices[i-1])
Run Code Online (Sandbox Code Playgroud)

查找所有索引的更快方法是使用numpy,如下所示:

import numpy as np
values = np.array(my_list)
searchval = '[tag]'
ii = np.where(values == searchval)[0]
print ii
Run Code Online (Sandbox Code Playgroud)

在相邻索引之间获得差异的另一种方法是使用itertools,

import itertools
diffs = [y-x for x, y in itertools.izip (indices, indices[1:])]
Run Code Online (Sandbox Code Playgroud)