1

I'm trying to count the occurrences of each character in the sentence. I used the code below:

printed = False
sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)
count = 0
for char in chars:
    if char == ' ':
        chars.remove(char)
    if printed == False:    
        count = chars.count(char)
        print "char count: ", char, count
    else:
        printed = False  

The problem is, that the first letter of each word, except for the first word is not printed and the count is incorrect (every time a new word starts, the count decrements by 1, starting from 7):

['t', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
char count:  t 2
char count:  h 2
char count:  e 3
char count:    7
char count:  u 2
char count:  i 1
char count:  c 1
char count:  k 1
char count:    6
char count:  r 2
char count:  o 4
char count:  w 1
char count:  n 1
char count:    5
char count:  o 4
char count:  x 1
char count:    4
char count:  u 2
char count:  m 1
char count:  p 1
char count:  s 1
char count:    3
char count:  v 1
char count:  e 3
char count:  r 2
char count:    2
char count:  h 2
char count:  e 3
char count:    1
char count:  a 1
char count:  z 1
char count:  y 1
char count:    0
char count:  o 4
char count:  g 1

When I create 2 for-loops, instead of 1, it works better:

sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)
count = 0
for char in chars:
    if char == ' ':
        chars.remove(char)
print chars
printed = False

for char in chars:
    if printed == False:
        count = chars.count(char)
        print "char count: ", char, count
        printed = True
    else:
        printed = False

And here's the output:

['t', 'h', 'e', 'q', 'u', 'i', 'c', 'k', 'b', 'r', 'o', 'w', 'n', 'f', 'o', 'x', 'j', 'u', 'm', 'p', 's', 'o', 'v', 'e', 'r', 't', 'h', 'e', 'l', 'a', 'z', 'y', 'd', 'o', 'g']
char count:  t 2
char count:  e 3
char count:  u 2
char count:  c 1
char count:  b 1
char count:  o 4
char count:  n 1
char count:  o 4
char count:  j 1
char count:  m 1
char count:  s 1
char count:  v 1
char count:  r 2
char count:  h 2
char count:  l 1
char count:  z 1
char count:  d 1
char count:  g 1

The only thing is, the 'o' character appears in the output twice... Why is that? Also, why won't 1 loop work?

Ekaterina1234
  • 400
  • 1
  • 7
  • 20

3 Answers3

2

Iterate over a copy of the list and use an elif or else just continue after the remove, you don't want to count empty spaces.

printed = False
sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)

for char in chars[:]:
    if char == ' ':
        chars.remove(char)
        continue
    if not printed:
        count = chars.count(char)
        print "char count: ", char, count
        printed = True
    else:
        printed = False

You could also just str.join after splitting on whitespace:

for char in "".join(sentence.split()):
    if not printed:
        count = chars.count(char)
        print "char count: ", char, count
        printed = True
    else:
        printed = False

But neither of your own solutions actually work correctly even making a copy of the list, you are missing letters in the output:

char count:  t 2
char count:  e 3
char count:  u 2
char count:  c 1
char count:  b 1
char count:  o 4
char count:  n 1
char count:  o 4
char count:  j 1
char count:  m 1
char count:  s 1
char count:  v 1
char count:  r 2
char count:  h 2
char count:  l 1
char count:  z 1
char count:  d 1
char count:  g 1

There are 26 unique letters in the string but you output ~17.

What you need is to keep track of the letters seen and only print the count once, your code keeps no record of what character has been printed it just randomly sets a flag:

sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)

printed = set()
for char in "".join(sentence.split()):
    if char not in printed:
        count = chars.count(char)
        print "char count: ", char, count
        printed.add(char)

Or if the order first seen does not matter then just call set on the string:

for char in set("".join(sentence.split())):
    count = chars.count(char)
    print "char count: ", char, count

Or if you had large amounts of data, you would be better with a Counter dict:

from collections import Counter
for char, count in Counter("".join(sentence.split())).items():
    print(char, count)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

your are changing the list chars over which are are iterating:

l = range(10)

for i in l:
    l.remove(i)
    print i

which gives:

0
2
4
6
8

you should not modifier the list you are iterating over. just skip the element which you do not want to process:

for char in chars:
    if char == ' ':
        continue

    if printed == False:    
        ...     
desiato
  • 1,122
  • 1
  • 9
  • 16
0

The problem arises because you remove the space char while the loop runs, and it disrupts the iteration process.

Simply don't do anything when a space char is encontered.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35