简体   繁体   中英

Splitting string based on list values into a list in Python

At the moment I'm trying to create a new list from a string, based on matching values from another list. For example my list of values is:

['IX', 'C', 'D', 'XL', 'I', 'XC', 'M', 'L', 'CD', 'X', 'IV', 'CM']

Which are all Roman numerals. How would I go about taking a Roman numeral:

MMIX

and splitting it to:

['M', 'M', 'IX']

based on the previous list.

Any help would be great!

I'm not sure this approach will work in general to parse roman numerals. For example, this code fails to properly parse VIII but that's because V isn't in the list of tokens. But here's a simple recursive function that looks for one of the tokens at the beginning of the input string and assembles a list:

tokens = ['IX', 'C', 'D', 'XL', 'I', 'XC', 'M', 'L', 'CD', 'X', 'IV', 'CM']

def rn_split(numeral, results_so_far=[]):
    if len(numeral)==0:
        return results_so_far # Break the recursion
    for token in tokens:
        if numeral.startswith(token):
            results_so_far.append(token)
            recurse_numeral = numeral[ (len(token)): ]
            return rn_split(recurse_numeral, results_so_far)
    # Remainder of numeral didn't match.  Bail out
    results_so_far.append(numeral)
    return results_so_far

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