one Liner: Lisprint(x) --> (a, b, c) instead of ['a', 'b', 'c']
Hen Hanna
henhanna at gmail.com
Sun Feb 26 04:54:02 EST 2023
On Saturday, February 25, 2023 at 11:45:12 PM UTC-8, Hen Hanna wrote:
> def Lisprint(x): print( ' (' + ', '.join(x) + ')' , '\n')
>
> a= ' a b c ? def f x if zero? x 0 1 '
> a += ' A B C ! just an example '
> x= a.split()
>
> print(x)
> Lisprint(x)
>
> ['a', 'b', 'c', '?', 'def', 'f', 'x', 'if', 'zero?', 'x', '0', '1', 'A', 'B', 'C', '!', 'just', 'an', 'example']
>
> (a, b, c, ?, def, f, x, if, zero?, x, 0, 1, A, B, C, !, just, an, example)
For nested lists.... impossible to improve upon P.Norvig's code
def Lisprint(x): print(lispstr(x))
def lispstr(exp):
"Convert a Python object back into a Lisp-readable string."
if isinstance(exp, list):
return '(' + ' '.join(map(lispstr, exp)) + ')'
else:
return str(exp)
a= ' a b c '
x= a.split()
x += [['a', 'b', 'c']]
x += x
print(x)
Lisprint(x)
['a', 'b', 'c', ['a', 'b', 'c'], 'a', 'b', 'c', ['a', 'b', 'c']]
(a b c (a b c) a b c (a b c))
---------- Without the commas, the visual difference (concision) is striking !
More information about the Python-list
mailing list