1

I am trying to count the number of T's, regardless of case from the provided list, but the code keeps counting every character and returning a value of 46, rather than just counting the T's and returning a value of 6. I also need it to print the strings in which the T's occur.

givenStrings = ["Taylor Swift", "Twenty Two", "Georgia Tech"]

count  =0
numTs = 0

for currentString in givenStrings:

    for currentCharacter in currentString:

        if currentCharacter == 't' or 'T':
            numTs+=1
            count = numTs + len(currentString)
            print(count, currentString)
Diggy.
  • 6,744
  • 3
  • 19
  • 38
Jman
  • 323
  • 4
  • 9
  • 2
    Possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – OneCricketeer Apr 04 '17 at 04:32
  • Not really sure what you expected `count = numTs + len(currentString)` to return you... – OneCricketeer Apr 04 '17 at 04:34
  • Try walking backward through your code and saying out loud what every line does. Or even better, [talk to a rubber duck](https://rubberduckdebugging.com/) – Charlie G Apr 04 '17 at 04:36
  • Thank you for your advice. I changed the count to: count = numTs + len(currentCharacter) after speaking out loud to myself. This was really by accident, so any explanation you might add about why this fixed the issue would be greatly appreciated. Thanks ahead of time! – Jman Apr 04 '17 at 05:56

5 Answers5

3

Your if condition should be instead:

if currentCharacter in ['t','T']:
Cyril
  • 3,048
  • 4
  • 26
  • 29
2

Change your if condition to

if currentCharacter == 't' or currentCharacter =='T':
jophab
  • 5,356
  • 14
  • 41
  • 60
0

Would be better not to iterate over the characters but to use sum with len and the string methods lower and count:

count = sum(len(currentString) for currentString in givenStrings)
numTs = sum(currentString.lower().count('t') for currentString in givenStrings)

The sum function can take any iterable. It sums.

The first applies len to each item in the list. Which sum consumes to produce a total.

The second uses the lower method to convert each of the strings to lowercase and then calls the count method to get the number of occurrences of the string 't'. Which sum consumes to produce a total.

In each of the uses, the argument is a generator expression (a for b in c) the parentheses can be omitted when it is the only argument to a function. sum(a for b in c) is the same as sum((a for b in c)). When it is not the only argument the parentheses are required.

There are other built-in functions that also take iterables most notably min, max, any, all. Note that min and max take a keyword argument key such that max((a for a in b), key=len) returns the item in the iterable that is the longest. The heapq.nsmallest and heapq.nlargest functions are effectively natural extensions of min and max. I'm fond of any and all especially when combined with not.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • Thank you so much for this, it worked really well! I hate to bother, but would you kindly break down for me what is happening here? Bit of a noob. Thanks again! – Jman Apr 04 '17 at 05:39
0

A simpler way would be to join the array elements into one string, make each character upper case, and then count the upper case T character

givenStrings = ["Taylor Swift", "Twenty Two", "Georgia Tech"]
print [x.upper() for x in ''.join(givenStrings)].count('T')

> 6
philshem
  • 24,761
  • 8
  • 61
  • 127
-1

I'd probably do it this way. Forget the nested loop

counts = map(lambda x: (x, x.lower().count('t'), ) , givenStrings) 
print(list(counts)) 
numTs = sum(x[1] for x in counts) 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245