Hello I am trying to write these python lines in a single line but getting some errors due to the dictionary modifications the code is doing.
for i in range(len(string)):
if string[i] in dict:
dict[string[i]] += 1
the general syntax I believe is
abc = [i for i in len(x) if x[i] in array]
Would it be possible for someone to tell me how this might work considering that I am adding 1 to the value in a dictionary
Thanks
What you’re trying to do can be done with
dict, a generator expression andstr.count():Alternative using
set(string)(from a comment down below bysoulcheck):Timing
Seen the comments down below I performed a little testing among this and other answers. (with python-3.2)
Test functions:
Test execution:
Test results: