pacoCham
pacoCham

Reputation: 48

How to alternate lower and upper case on dictionary keys? Python 3

im new on python 3.

What I want to do is to alternate upper and lowercase but only on a dictionary key.

my dictionary is created from a list, its key is the word (or list element) and its value is the times this element appears in the list.

kb     = str(input("Give me a string: "));
txt    = kb.lower();      #Turn string into lowercase
cadena = txt.split();     #Turn string into list
dicc   = {};              

for word in cadena:
         if (word in dicc):
             dicc[word] = dicc[word] + 1
         else:
             dicc[word] = 1
print(dicc)

With this code i can get for example:

input: "Hi I like PYthon i am UsING python"
{'hi': 1, 'i': 2, 'like': 1, 'python': 2, 'am': 1, 'using': 1}

but what I am trying to get is actually is:

{'hi': 1, 'I': 2, 'like': 1, 'PYTHON': 2, 'am': 1, 'USING': 1}

I tried using this:

for n in dicc.keys():
    if (g%2 == 0):
        n.upper()

    else:
        n.lower()
print(dicc)

But it seems that I have no idea of what I'm doing. Any help would be appreciated.

Upvotes: 1

Views: 2297

Answers (2)

user3483203
user3483203

Reputation: 51165

Using itertools and collections.OrderedDict (to guarantee order in Python < 3.7)

Setup

import itertools
from collections import OrderedDict

s = 'Hi I like PYthon i am UsING python'
switcher = itertools.cycle((str.lower, str.upper))
d = OrderedDict()
final = OrderedDict()

First, create an OrderedDictionary just to count the occurences of strings in your list (since you want matches to be case insensitive based on your output):

for word in s.lower().split():
    d.setdefault(word, 0)
    d[word] += 1

Next, use itertools.cycle to call str.lower or str.upper on keys and create your final dictionary:

for k, v in d.items():
    final[next(switcher)(k)] = v

print(final)

OrderedDict([('hi', 1), ('I', 2), ('like', 1), ('PYTHON', 2), ('am', 1), ('USING', 1)])

Upvotes: 1

shadowtalker
shadowtalker

Reputation: 13833

Your n in dicc.keys() line is wrong. You are trying to use n as both the position in the array of keys and the key itself.

Also the semicolons are unnecessary.

This should do what you want:

from collections import OrderedDict

# Receive user input

kb     = str(input("Give me a string: "))
txt    = kb.lower()
cadena = txt.split()
dicc   = OrderedDict()

# Construct the word counter

for word in cadena:
    if word in dicc:
        dicc[word] += 1
    else:
        dicc[word] = 1

If you just want to print the output with alternating case, you can do something like this:

# Print the word counter with alternating case

elems = []
for i, (word, wordcount) in enumerate(dicc.items()):
    if i % 2 == 0:
        word = word.upper()
    elems.append('{}: {}'.format(word, wordcount)

print('{' + ', '.join(elems) + '}')

Or you can make a new OrderedDict with alternating case...

dicc_alt_case = OrderedDict((word.upper() if (i % 2 == 0) else word, wordcount)
                            for word, wordcount in dicc.items())

Upvotes: 0

Related Questions