0

Before you close this, please try to understand the question - it's a bit different, but looks like a lot of downvoted questions.

I'm looking for a simple way to check if any substrings of a string is in a list of strings. The input is like 2018_JAN_BGD, and I want to convert the month info in this to a (year-)quarter info. For example Q1 would be January, February and March. So I'm looking for a way to check if any of the substrings in 2018_JAN_BGD contains JAN, FEB or MAR. I could do this by writing 12 (for the whole year) if-else clauses with or's, but I feel like there's a more elegant solution in Python. I'm trying:

def tellQuarter(month):
    Q1 = ('JAN','FEB','MAR')
    Q2 = ('APR','MAY','JUN')
    Q3 = ('JUL','AUG','SEP')
    Q4 = ('OCT','NOV','DEC')
    if any(for mon in Q1 in month):
        return ("Q1")
    if any(for mon in Q2 in month):
        return ("Q2")

This is of course incorrect (invalid syntax), but I can't figure out how to put this correctly. Basically, I want to loop through Q1, and check if any of the strings is a substring of month.

I mean I can do

for mon in Q1:
    if mon in month:
        return "Q1"

but isn't there a more elegant, single-line solution to this?

lte__
  • 7,175
  • 25
  • 74
  • 131
  • Is the month always at the same position in the string? Could you have something like `2018_MAR_SEP'? – Thierry Lathuille Feb 20 '18 at 12:24
  • Hm I'd have to check whether it's at the same position, not sure (that's why I'm going with `in`). You cannot have anything like `2018_MAR_SEP. – lte__ Feb 20 '18 at 12:26
  • As asked, the question is a duplicate. However, the appropriate way to solve the underlying problem involves completely different logic. We *know which part* of the input string will have the month abbreviation, so we don't need or want to search for it as a substring - just extract that part, and look up which month it represents. – Karl Knechtel Aug 02 '22 at 23:53

3 Answers3

3

Cheating, but use a dictionary, mapping the months to quarters:

monthsToQuarters = {
    'JAN': 'Q1','FEB': 'Q1','MAR': 'Q1',
    'APR': 'Q2','MAY': 'Q2','JUN': 'Q2',
    'JUL': 'Q3','AUG': 'Q3','SEP': 'Q3',
    'OCT': 'Q4','NOV': 'Q4','DEC': 'Q4'}

giving you:

>>> monthsToQuarters['JAN']
'Q1'

you could strip out the month using something like:

>>> '2018_JAN_BGD'.split('_')[1]
'JAN'
Lanting
  • 3,060
  • 12
  • 28
2

Avoid repeating logic with a dict:

QUARTERS = {
    'Q1': ('JAN', 'FEB', 'MAR'),
    'Q2': ('APR', 'MAY', 'JUN'),
    'Q3': ('JUL', 'AUG', 'SEP'),
    'Q4': ('OCT', 'NOV', 'DEC'),
}

def tellQuarter(month):
    for q, months in QUARTERS.items():
        if any(m for m in months if m in month):
            return q
    raise ValueError("String '{}' does not contain a month.".format(month))

print(tellQuarter('2018_JAN_BGD'))
>>> Q1
jdehesa
  • 58,456
  • 7
  • 77
  • 121
1

You had a syntax error in your list comprehension

def tellQuarter(month):
        Q1 = ('JAN','FEB','MAR')
        Q2 = ('APR','MAY','JUN')
        Q3 = ('JUL','AUG','SEP')
        Q4 = ('OCT','NOV','DEC')
        if any(mon in month for mon in Q1):
            return ("Q1")
        if any(mon in month for mon in Q2):
            return ("Q2")

print tellQuarter("2018_JAN_BGD")

Output:

Q1
Rakesh
  • 81,458
  • 17
  • 76
  • 113