Damion Pointin
Damion Pointin

Reputation: 19

Python lists and and loops

I need to print out the number of a's, b's, c's etc. in the following statement and have it cascade down and have the number of times it appeared in the statement next to it.

"you can have data without information, but you cannot have information without data."

I have got so far and could do with some help?

This is day 3 of me learning python so take it easy.

from collections import defaultdict

text = 'You can have data without information, but you cannot have information without data.'
lowerCaseText = text.lower()

print(lowerCaseText)
chars = defaultdict(int)

for char in text:
chars[char] += 1

print(chars)

It needs to look like this

a : 10
b : 1
c : 2
d : 2
e : 2
f : 2
h : 4
i : 6
m : 2
n : 7
o : 9
r : 2
t : 10
u : 5
v : 2
w : 2
y : 2

Upvotes: 1

Views: 410

Answers (10)

Arifa Chan
Arifa Chan

Reputation: 1015

This 3 workarounds uses ascii_lowercase from string module.

First, print while iterating:

from string import ascii_lowercase
text = "You can have data without information, but you cannot have information without data."

for char in ascii_lowercase:
    count = 0
    for letter in text.lower():
        if letter == char:
            count += 1
    if count: 
        print(f'{char} : {count}')

Second, using dictionary to update the counted value using dict.update():

from string import ascii_lowercase
text = "You can have data without information, but you cannot have information without data."

count = dict()
for char in ascii_lowercase:
    if char in text:
        count.update({char: text.lower().count(char)})
for char in count:
    print(f'{char} : {count[char]}')

Third, using dict comprehension:

from string import ascii_lowercase
text = "You can have data without information, but you cannot have information without data."
count = {char: text.lower().count(char) for char in ascii_lowercase if char in text}
for char in count: print(f'{char} : {count[char]}')

Results:

a : 10
b : 1
c : 2
d : 2
e : 2
f : 2
h : 4
i : 6
m : 2
n : 7
o : 9
r : 2
t : 10
u : 5
v : 2
w : 2
y : 2

Upvotes: 1

dspr
dspr

Reputation: 2423

We can take advantage that a character is also a number: subtracting 97 from the ascii code gives us an number between 0 and 25 that we can use as the index of a list:

text = 'You can have data without information, but you cannot have information without data'.lower()
lst  = [0] * 26

#counts iterations
for char in text:
    if not char.isalpha(): continue
    lst[ord(char) - 97] += 1
 
#prints the result:
for i in range(26):
    if lst[i] > 0:
        string = chr(i + 97) + " : " + str(lst[i])
        print(string)

#=>
a : 10
b : 1
c : 2
d : 2
e : 2
f : 2
h : 4
i : 6
m : 2
n : 7
o : 9
r : 2
t : 10
u : 5
v : 2
w : 2
y : 2

Upvotes: 1

Kdhere
Kdhere

Reputation: 125

Nice problem here is solution for your defaultdict Please let me know where are you learning python problems!

from collections import defaultdict
text = 'You can have data without information, but you cannot have information without data.'

lowerCaseText = text.lower()
chars = defaultdict(int)

for char in lowerCaseText:
   if char.isalpha():      # Only taking alphabet removing . , space
      chars[char] += 1


for i, j in sorted(chars.items()):
   print(i, ":", j)

Upvotes: 1

driesaster
driesaster

Reputation: 25

first you need to filter out everything that is not a letter from your string. You also need to preferably sort your string before you convert it into a dict so that the values get added in the right order. It's also possible to sort the dict afterwards but that's more difficult in this case.

We do this using this line of code:

filteredText = sorted(''.join(filter(str.isalnum, text)).lower())

(str.isalnum, text) removes all non letters but it comes out as a list so we need to join it later again.

then when we need to go over the text and check if it is already in our dictionary. If not we need to add it with value 1, otherwise we increment with 1.

Full code:

text = 'You can have data without information, but you cannot have information without data.'
filteredText = sorted(''.join(filter(str.isalnum, text)).lower())

chars = {}

for char in filteredText:
  if char in chars:
      chars[char] += 1
  else :
      chars[char] = 1

print(chars)

Upvotes: 1

R. Baraiya
R. Baraiya

Reputation: 1530

Code:

import re      #This package to remove space and special char from text
import string  #This package to alphabets


text = 'You can have data without information, but you cannot have information without data.'
Alpha = dict.fromkeys(string.ascii_lowercase, 0)   #Output {a:0,b:0 ..}

for t in re.sub('[^A-Za-z0-9]+', '', text):        #using re avoiding the spaces and special characters
    Alpha[t.lower()] = Alpha[t.lower()]+1          #each time add 1 on specific alphabet
    
Alpha

Output:

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

To get your desire output {k:v for k,v in Alpha.items() if v>0} output:

{'a': 10, 'b': 1, 'c': 2, 'd': 2, 'e': 2, 'f': 2, 'h': 4, 'i': 6, 'm': 2, 'n': 7, 'o': 9, 'r': 2, 't': 10, 'u': 5, 'v': 2, 'w': 2, 'y': 2}

Upvotes: 1

Ali Yeşilkanat
Ali Yeşilkanat

Reputation: 607

You can use collections.Counter. I also used collections.OrderedDict to sort values.

from collections import Counter, OrderedDict

text = 'You can have data without information, but you cannot have information without data.'
lower_case_text = text.lower()
lower_case_text = ''.join(filter(str.isalnum, lower_case_text)) # this line is required for removing non alpha numeric characters.
counter = Counter(lower_case_text)
counter_ordered=OrderedDict(counter.most_common())
for k, v in counter_ordered.items():
    print(k + " : " + str(v))

Upvotes: 1

Mike R
Mike R

Reputation: 589

Here’s a different and rough idea for a different way to do things:

So what you could do is have an array of 26 values, 1 for each letter of the alphabet. And then a 2nd array with 26 values, all starting with 0. So then you can loop through the 1st array, figure out how many times that value is found in the text, and then set that value in the 2nd array.

a = [‘a’, ‘b’, to ‘z’] b = [0,0,0,…0]

for each letter in the alphabet: B[character value] = text.count(a[character value])

Then you could do a for loop to get things printed the way you want.

Upvotes: 1

Tanner
Tanner

Reputation: 181

list(set(lowerCaseText) will return all of the unique characters in your string.

You can then go through those unique characters and get a count of their occurrences in your initial string by using lowerCaseText.count(char)

Put it all together and you get something like this:

text = 'You can have data without information, but you cannot have information without data.'
lowerCaseText = text.lower()
uniq = sort(list(set(lowerCaseText)))
for char in uniq:
    print(char, ':', lowerCaseText.count(char))

Upvotes: 1

opitimus
opitimus

Reputation: 55

I think you asked for occurences in a string ,first example is occurences of the words in a list,and the other example is occurences of letters in a string using dictionary but you can also use Counter there!

First Example:

   from collections import Counter

   text = 'You can have data without information, but you cannot have information without data.'
   text = text.split()
   text = Counter(text)
   print(text)

Second Example:

    text = 'You can have data without information, but you cannot have information 
without data.'
    text = text.lower()
    text_dictionary = {}
    for element in text:
        if element not in text_dictionary and element.isalpha():
            text_dictionary[element] = 1
        else:
            if element.isalpha():
                text_dictionary[element] += 1
    print(text_dictionary)

Upvotes: 1

Thomas Kimber
Thomas Kimber

Reputation: 11067

If you're doing a homework assignment, then this isn't going to help - but if you're looking for a quick way to achieve your goal, then the Counter function from the collections module should be of interest:

text = 'You can have data without information, but you cannot have information without data.'
from collections import Counter
Counter(text.lower())

>> Counter({ "y" : 2, 
             "o" : 9, 
             "u" : 5,
             ...etc

Upvotes: 2

Related Questions