I'm new to python and would like to know if there is an easy way to search for Strings in array that have the same starting characters.
for example I have a list
ex = [exA, exB, teA, exC]
and want to get result for everything matching the first two characters
something like this:
{'ex' : 3, 'te' : 1}
I have tried working with the Counter method from collections but I cant get a result as shown above.
thank you in advanced
If you slice off the first two characters of each element you can use collections.Counter
for this
>>> import collections
>>> ex = ['exA', 'exB', 'teA', 'exC']
>>> collections.Counter(i[:2] for i in ex)
Counter({'ex': 3, 'te': 1})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With