Using python to find objects in array with same starting characters -
i'm new python , know if there easy way search strings in array have same starting characters.
for example have list
ex = [exa, exb, tea, exc]
and want result matching first 2 characters this:
{'ex' : 3, 'te' : 1}
i have tried working counter method collections cant result shown above.
thank in advanced
if slice off first 2 characters of each element can use collections.counter
this
>>> import collections >>> ex = ['exa', 'exb', 'tea', 'exc'] >>> collections.counter(i[:2] in ex) counter({'ex': 3, 'te': 1})
wiki
Comments
Post a Comment