我有2个这样的列表:
prefix = ['b', 'bo', 'br']
word = ['bring', 'boring', 'bold', 'bells']
如何计算列表词中每个前缀多少次?
它应该返回
[4, 2, 1]
使用列表理解,str.startswith
以及sum
[sum(w.startswith(p) for w in word) for p in prefix] # [4, 2, 1]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句