Count specific individual letters within a list of strings [duplicate]
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?
python python-3.x
marked as duplicate by Prune
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.
add a comment |
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?
python python-3.x
marked as duplicate by Prune
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
add a comment |
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?
python python-3.x
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
python python-3.x
edited Nov 13 '18 at 0:17
JameshGong
asked Nov 13 '18 at 0:13
JameshGongJameshGong
486
486
marked as duplicate by Prune
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 13 '18 at 0:24
answered Nov 13 '18 at 0:17
BenBen
2,67021524
2,67021524
add a comment |
add a comment |
This question is slightly different than the one you used to mark as duplicate.
– Naidu
Nov 13 '18 at 0:20