如何将列表中的字符串位置添加到新的双精度列表中?

时间:2016-11-16 11:11:35

标签: python list python-3.x count position

示例:

r是列在列表中的文本文件

r = ['John is american', 'Bea is french', 'John is american', 'Ray is german', 'John is american', 'Bea is french', 'Bea is french', '', 'Lisa is dutch']

我想要做的是计算出现次数并在r:

中添加位置
finallist = ['string', frequency, [positions in r]]

finallist = [['John is american', 3, [0,2,4]], ['Bea is french', 3, [1,5,6]], ['Ray is german', 1, [3]], ['Lisa is dutch', 1, [7]]]

我知道如何计算r:

中的字符串
[[x,r.count(x)] for x in set(r)]

(或使用集合库中的Counter类)

但是如何将r中字符串的位置添加到finallist?

1 个答案:

答案 0 :(得分:1)

使用字典来跟踪句子的位置(建筑物清单);这些列表的最终长度也是频率计数:

from collections import defaultdict

pos = defaultdict(list)
for i, sentence in enumerate(r):
    pos[sentence].append(i)
finallist = [[sentence, len(positions), positions] for sentence, positions in pos.items()]

演示:

>>> from collections import defaultdict
>>> r = ['John is american', 'Bea is french', 'John is american', 'Ray is german', 'John is american', 'Bea is french', 'Bea is french', '', 'Lisa is dutch']
>>> pos = defaultdict(list)
>>> for i, sentence in enumerate(r):
...     pos[sentence].append(i)
...
>>> [[sentence, len(positions), positions] for sentence, positions in pos.items()]
[['John is american', 3, [0, 2, 4]], ['Bea is french', 3, [1, 5, 6]], ['Ray is german', 1, [3]], ['', 1, [7]], ['Lisa is dutch', 1, [8]]]

如果输出顺序很重要,并且您还没有访问Python 3.6(在此答案时为in betadict实现保留了插入顺序),那么您可以使用OrderedDict实例,并使用dict.setdefault()来实现每个键的初始空列表:

from collections import OrderedDict

pos = OrderedDict()
for i, sentence in enumerate(r):
    pos.setdefault(sentence, []).append(i)
finallist = [[sentence, len(positions), positions] for sentence, positions in pos.items()]