On 2024-05-30 21:47:14 -0700, HenHanna via Python-list wrote:
[('the', 36225), ('and', 17551), ('of', 16759), ('i', 16696), ('a', 15816), ('to', 15722), ('that', 11252), ('in', 10743), ('it', 10687)]
((the 36225) (and 17551) (of 16759) (i 16696) (a 15816) (to 15722) (that 11252) (in 10743) (it 10687))
i think the latter is easier-to-read, so i use this code
(by Peter Norvig)
This doesn't work well if your strings contain spaces:
Lprint(
[
["Just", "three", "words"],
["Just", "three words"],
["Just three", "words"],
["Just three words"],
]
)
prints:
((Just three words) (Just three words) (Just three words) (Just three words))
Output is often a compromise between readability and precision.
def lispstr(exp):
# "Convert a Python object back into a Lisp-readable string."
if isinstance(exp, list):
This won't work for your example, since you have a list of tuples, not a
list of lists and a tuple is not an instance of a list.
return '(' + ' '.join(map(lispstr, exp)) + ')'
else:
return str(exp)
def Lprint(x): print(lispstr(x))
I like to use pprint, but it's lacking support for user-defined types. I
should be able to add a method (maybe __pprint__?) to my classes which
handle proper formatting (with line breaks and indentation).
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | |
hjp@hjp.at | -- Charles Stross, "Creative writing
__/ |
http://www.hjp.at/ | challenge!"
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmZa2qkACgkQ8g5IURL+ KF0k4RAArl1ScfmObpeO7EUmZvAPDiwSIFvR75UAbVC/afrc6i1mDVoUwXV2MjLS 8B6wK5/yyrxm9JLQbboAH3VIPM7/pKYWAN6KqJAIyyn2LVkgmrFnQ3WoRhg3GjtM Nx6GCH7VomVyLz86Qj/Ed0UuxFmNBjDa0ykuxUobchP8GV88yJ3F7sYqrQA+zCkW fkLyjpsj531yowus8X2r31tbdTlVS6vYnZ94+ULRWSz621O+oTKD50Jcdr4pmof/ PnrEmMxOaHNJaEM9QnIHMdtsYyc4lkfES/B8oKGYb9nDxITgOhYV19DMSBMYxyvA CYi9c9OotA5hwpcUxnxgC/ZQmiZqtNPal0SAKtS3IxWUUZXXwTm4k2lTVaiDoV+o b4u1FpzSEc289+5gzD+MRMS6xij3Cd61vMu4imK9HTS6Xngbs8uAX/DVVQ6uzHWj alfyjHurYGoE7X+Ffw5OoM9BdSu7ekw6p/lRLd0FAW7rDVK+Mfzwm+uXrGJvGPKv zkzAuLsROVwHpewodhSlTLGrXdQYEiFAaTNSofexPVjlp8QX/7kSBzG6PJFx6Ttr FHfWB7vL+25jf+4m+Ap1m4llF+8AzAv1GO/vDc55og8e0oN+ijtsat07oFaVX0o0 2pGAyBOlbt9UHvQeP7JQhNJchrnOcc4h5lEq8aI