简体   繁体   中英

Count number of capital letters in file Python?

I am trying to count the number of unique capital letter from a text file which contains text data. I have done with counting the capital letter from file but need your help in how to count unique capital letters.

This is what i have tried yet:

with open('demo.txt') as countletter:
    count = 0
    text = countletter.read()
    for character in text:
        if character.isupper():
            count += 1
print(count) 

You need to keep track of capital letters that already appeared. Using set is a feasible solution.

letters = set()
with open('demo.txt') as countletter:
    count = 0
    text = countletter.read()
    for character in text:
        if character.isupper():
            count += 1
            letters.add(character)
print(count)

Very compact solution:

with open('demo.txt') as countletter:
    count = sum(character.isupper() for character in set(countletter.read()))
    print(count)

solution using set

unique_capital_letters = set()
with open('demo.txt') as countletter:
    text = countletter.read()
    for character in text:
        if character.isupper():
            unique_capital_letters.add(character)

number_of_unique_occurrences = len(unique_capital_letters)
print(number_of_unique_occurrences)

Add all the characters to a list. Then create a set out of it and take it length. This will give you the unique Capital Characters. Something like below:

with open('demo.txt') as countletter:
    char_list = list()
    count = 0
    text = countletter.read()
    for character in text:
        if character.isupper():
            #append the capitals
            char_list.append(character)

#create a set and get its length
print(len(set(char_list))) 

You also need to record all the capitals letter that have already appeared.

Something like

CapitalChar=''
with open('demo.txt') as countletter:
    count = 0
    text = countletter.read()
    for character in text:
        if character.isupper() and not in CapitalChar:
            CapitalChar=CapitalChar + character
            count += 1
print(count) 

The other answers are fine, just wanted to point out that you can use a Counter to make life much easier:

from collections import Counter

with open('demo.txt') as countletter:
    unique = Counter(countletter.read())
    unique_capital_letters = {char: count for char, count in unique.items() if char.isupper()}

There are already some perfect answers posted here, but just for convenience here is a very compact way to achieve it with just regular python:

with open('demo.txt') as countletter:
  count = sum(character.isupper() for character in set(countletter.read()))

A set guarantees every element is unique, and sum used like I did ads one to count each time the character is uppercase

Similar to @Eric's answer, but with the map function instead:

with open('demo.txt') as countletter:
    count = sum(map(str.isupper, set(countletter.read())))

There are many ways to achieve your goal. People are surely giving you a way based on the logic of your code. Here, I give you a new way to achieve your goal by using list comprehensions synthax.

with open('demo.txt') as f:
   text = f.read()
   count = len(set([character for character in text if character.isupper()]))

print(count)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM