Count specific individual letters within a list of strings [duplicate]










3















This question already has an answer here:



  • Count multiple letters in string Python

    3 answers



Note: NOT a duplicate..I want to know the individual counts of each letter, the one you posted as duplicate gives the total count of each one.



I am trying to count the individual letters from all the strings which are stored in a list.



def countElement(a):
g =
for i in a:
if i in g:
g[i] +=1
else:
g[i] =1
return g

list : ['a a b b c c', 'a c b c', 'b c c a b']



for i in range(1000000):
b = countElement(list)
print(b)


At the moment this produces the result:



'a a b b c c': 1, 'a c b c': 1, 'b c c a b': 1


But the result that I actually want to achieve is:



a = 4
b = 5
c = 6


Is there a different function that I can use which will count the individual letters within the strings in the list?










share|improve this question















marked as duplicate by Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 0:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • This question is slightly different than the one you used to mark as duplicate.
    – Naidu
    Nov 13 '18 at 0:20















3















This question already has an answer here:



  • Count multiple letters in string Python

    3 answers



Note: NOT a duplicate..I want to know the individual counts of each letter, the one you posted as duplicate gives the total count of each one.



I am trying to count the individual letters from all the strings which are stored in a list.



def countElement(a):
g =
for i in a:
if i in g:
g[i] +=1
else:
g[i] =1
return g

list : ['a a b b c c', 'a c b c', 'b c c a b']



for i in range(1000000):
b = countElement(list)
print(b)


At the moment this produces the result:



'a a b b c c': 1, 'a c b c': 1, 'b c c a b': 1


But the result that I actually want to achieve is:



a = 4
b = 5
c = 6


Is there a different function that I can use which will count the individual letters within the strings in the list?










share|improve this question















marked as duplicate by Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 0:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • This question is slightly different than the one you used to mark as duplicate.
    – Naidu
    Nov 13 '18 at 0:20













3












3








3








This question already has an answer here:



  • Count multiple letters in string Python

    3 answers



Note: NOT a duplicate..I want to know the individual counts of each letter, the one you posted as duplicate gives the total count of each one.



I am trying to count the individual letters from all the strings which are stored in a list.



def countElement(a):
g =
for i in a:
if i in g:
g[i] +=1
else:
g[i] =1
return g

list : ['a a b b c c', 'a c b c', 'b c c a b']



for i in range(1000000):
b = countElement(list)
print(b)


At the moment this produces the result:



'a a b b c c': 1, 'a c b c': 1, 'b c c a b': 1


But the result that I actually want to achieve is:



a = 4
b = 5
c = 6


Is there a different function that I can use which will count the individual letters within the strings in the list?










share|improve this question
















This question already has an answer here:



  • Count multiple letters in string Python

    3 answers



Note: NOT a duplicate..I want to know the individual counts of each letter, the one you posted as duplicate gives the total count of each one.



I am trying to count the individual letters from all the strings which are stored in a list.



def countElement(a):
g =
for i in a:
if i in g:
g[i] +=1
else:
g[i] =1
return g

list : ['a a b b c c', 'a c b c', 'b c c a b']



for i in range(1000000):
b = countElement(list)
print(b)


At the moment this produces the result:



'a a b b c c': 1, 'a c b c': 1, 'b c c a b': 1


But the result that I actually want to achieve is:



a = 4
b = 5
c = 6


Is there a different function that I can use which will count the individual letters within the strings in the list?





This question already has an answer here:



  • Count multiple letters in string Python

    3 answers







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 0:17







JameshGong

















asked Nov 13 '18 at 0:13









JameshGongJameshGong

486




486




marked as duplicate by Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 0:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 0:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • This question is slightly different than the one you used to mark as duplicate.
    – Naidu
    Nov 13 '18 at 0:20
















  • This question is slightly different than the one you used to mark as duplicate.
    – Naidu
    Nov 13 '18 at 0:20















This question is slightly different than the one you used to mark as duplicate.
– Naidu
Nov 13 '18 at 0:20




This question is slightly different than the one you used to mark as duplicate.
– Naidu
Nov 13 '18 at 0:20












1 Answer
1






active

oldest

votes


















2














Sure! Use collections.Counter:



from collections import Counter


lst = ['a a b b c c', 'a c b c', 'b c c a b']

counter = Counter()
for word in lst:
counter.update(word)

print(counter)
# Counter(' ': 12, 'c': 6, 'b': 5, 'a': 4)





share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Sure! Use collections.Counter:



    from collections import Counter


    lst = ['a a b b c c', 'a c b c', 'b c c a b']

    counter = Counter()
    for word in lst:
    counter.update(word)

    print(counter)
    # Counter(' ': 12, 'c': 6, 'b': 5, 'a': 4)





    share|improve this answer



























      2














      Sure! Use collections.Counter:



      from collections import Counter


      lst = ['a a b b c c', 'a c b c', 'b c c a b']

      counter = Counter()
      for word in lst:
      counter.update(word)

      print(counter)
      # Counter(' ': 12, 'c': 6, 'b': 5, 'a': 4)





      share|improve this answer

























        2












        2








        2






        Sure! Use collections.Counter:



        from collections import Counter


        lst = ['a a b b c c', 'a c b c', 'b c c a b']

        counter = Counter()
        for word in lst:
        counter.update(word)

        print(counter)
        # Counter(' ': 12, 'c': 6, 'b': 5, 'a': 4)





        share|improve this answer














        Sure! Use collections.Counter:



        from collections import Counter


        lst = ['a a b b c c', 'a c b c', 'b c c a b']

        counter = Counter()
        for word in lst:
        counter.update(word)

        print(counter)
        # Counter(' ': 12, 'c': 6, 'b': 5, 'a': 4)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 0:24

























        answered Nov 13 '18 at 0:17









        BenBen

        2,67021524




        2,67021524













            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto