vixero
vixero

Reputation: 514

Regex: convert a string to a dictionary using dict comprehension and regex

I a have a string which i want to convert to a dictionary. What i want to do is select the uppercase chars as keys for the dictionary and count them up as values. If an uppercase char is followed by a lowercase (or several ones in a row), it should go as a new key. Assume the string is IIrIIrIrIrIIrIIrIrIrII, then the output should be the following: {'I': 6, 'Ir': 8}. Instead, I get {'Ir': 8, 'I': 14}.

This is what i have:

def convert(string):
    return {el: string.count(el) for el in re.findall('[A-Z][a-z]*', string)}

I got stuck at the regex part. If the string were to be AIrAIrIrIrAIrAIrIrIrAA, then I get the correct output.

Pls help, thx

Upvotes: 0

Views: 202

Answers (1)

Cyphase
Cyphase

Reputation: 12002

Use Counter:

from collections import Counter

def convert(string):
    return Counter(re.findall('[A-Z][a-z]*', string))

Example:

>>> convert('IIrIIrIrIrIIrIIrIrIrII')
Counter({'Ir': 8, 'I': 6})

You can wrap that return in dict() if you don't want to return a Counter. Or just do it outside the function if you need to.

The problem in your code is that the count is coming from checking string.count(thing), so you're counting all the 'I''s, including the ones that are part of 'Ir'.

Upvotes: 1

Related Questions