Before explaining my requirement, let me explain what I don't want:
s= 'Hello satya, have you achieved your goal'
s.count('l') #o/p: 3
#I don't want 3 instead I need 2. How? explained below.
Expected Output:
{'H':3, 'e':3, 'l':2, 'o':4, 's':1, 'a':3....etc}
That is : take all unique letters/characters from that sentence. Then find "in how many words that character is present" , so count that no of occurence and store in dictionary.
For example character 'l' is present 3 times in that sentence, but present in 2 WORDS(in hello it is repeated but I want it to be counted as 1).
Please suggest. If i missed something to make my problem more understandable, please highlight.
Here is what I tried:
def char_word(s):
s_d = {}
chars = list(s.replace(' ', ''))
print(chars)
for char in chars:
c_count = 0
for word in s.split():
if char in word:
c_count += 1
s_d[char] = c_count
print(s_d)
return s_d
You can do
sum(['l' in i for i in s.split()])
s.split()
splits your sentence into words (it splits by ). Then the for-loop checks if the character
l
is in that particular word, yielding True
or False
if it is or is not. The sum
function counts all the True
s
First, split the string into individual words and create the output dictionary. I recommend using the collections.Counter
c.ass, which is just a dict
subclass designed to count occurrences:
import collections
counter = collections.Counter()
words = s.split()
Now loop through all of the words and create a set
from each word. Since set
can't have duplicate values, it's guaranteed to have exactly one copy of each characters:
for word in words:
characters = set(word)
And finally, loop through the now unique characters of the word, incrementing the counter's value for that character:
for character in characters:
counter[character] += 1
Also, you can shorten this quite a lot, even down to a one-liner:
>>> counter = collections.Counter(c for word in s.split() for c in set(word))
To get the count of a particular character, just access it through counter[c]
:
>>> counter['l']
2
>>> counter['x']
0
>>> counter['H']
1
You can do it, too, within groupby
from itertools
module and dict comprenesion
like this way:
from itertools import groupby
a = 'Hello satya, have you achieved your goal'
a = a.replace(',','').split()
sub = [j.lower() for k in a for j in set(k)]
final = {k:len(list(v)) for k,v in groupby(sorted(sub), lambda x: x)}
print(final)
Outut:
{'h': 3, 'u': 2, 's': 1, 'i': 1, 'c': 1, 'a': 4, 'd': 1, 'e': 3, 't': 1, 'v': 2, 'l': 2, 'o': 4, 'r': 1, 'g': 1, 'y': 3}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.