(Is this really the best way?) (defun ro2ar (RN) My solution: Write a wrapper function to convert a string into symbol or list of symbols as required for the funciton linked above. When dealing with web data, file names, or most other sources the roman numerals will most likely be in an Emacs Lisp string object. Unfortunately this function expects the number to be encoded as one Emacs Lisp symbol per digit, or a list of such symbol encoded digits. Here's the most applicable solution I found via a general web search: I decided it should be doable in Lisp, but if Emacs contains any function to do the conversion, I could not find it. Such numbers are not very useful for sorting and comparing with numbers in other forms. I gather data from all kinds of sources, and occasionally people use numbers written as roman numerals. Answers written as one or more Emacs Lisp functions would be ideal, thanks! I would like to accomplish this with Emacs Lisp, so I'm not expecting answers written as keyboard macros, other Lisp dialects, or shell scripts. This last solution will work on any list stored as a string, nested or not.In Emacs Lisp, how to write an Emacs Lisp function to convert a natural number from roman numerals representation to the equivalent Emacs Lisp integer value? The Emacs Lisp function should be callable from Emacs Lisp and return an Emacs Lisp integer object for further processing in an Emacs Lisp program. # Flatten and split the non nested list items # Look ahead for the bracketed text that signifies nested list # Clean it up so the regular expression is simpler If you need to retain the nested list it gets a bit uglier, but it can still be done just with regular expressions and list comprehension: import re Without using regex (which would simplify the replace), and assuming you want to return a flattened list (and the zen of python says flat is better than nested): x = ']' If you have nested lists, it does get a bit more annoying. You can parse and clean up this list as needed using list comprehension. # Replace, as split will otherwise retain the quotes in the returned list # String indexing to eliminate the brackets. You can do this in one line for most basic use cases, including the one given in the original question. There isn't any need to import anything or to evaluate. for the type of workloads I use Python for I usually value readability over a slightly more performant option, but as usual it depends. there are trade-offs to consider when going with the most readable option. I was disappointed to see what I considered the method with the worst readability was the method with the best performance. Timeit.timeit(stmt="list(map(str.strip, json.loads(u'')))", setup='import json', number=100000) Timeit.timeit(stmt="list(map(str.strip, ast.literal_eval(u'')))", setup='import ast', number=100000) List(map(str.strip, ast.literal_eval(u''))) Inspired from some of the answers above that work with base Python packages I compared the performance of a few (using Python 3.7.3):
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |