WolframResearch / WolframClientForPython

Call Wolfram Language functions from Python
https://wolfr.am/wolframclientdoc
MIT License
446 stars 47 forks source link

How to convert back "WolframObjects" to python objects #32

Closed sla-te closed 1 year ago

sla-te commented 2 years ago

e.g. Table[Values[i], {i, WordData["motorbus", "Synonyms"]}] Id like to have a python List.. As of now I am doing it like this (which seems very hacky)

raw_results = evaluate('ExportString[Table[Values[i], {i, WordData["motorbus", "Synonyms"]}], "CSV"]')
result_list = results.replace('"', '').replace('\n', '').split(',')
riccardodivirgilio commented 1 year ago

Hi, this is documented here:

https://reference.wolfram.com/language/WolframClientForPython/docpages/basic_usages.html#expression-evaluation

by default you are getting back tuples, that are immutable. you can convert them to list with python code.

>>> from wolframclient.evaluation import WolframLanguageSession
>>> session = WolframLanguageSession()
>>> res = session.evaluate('Table[Values[i], {i, WordData["motorbus", "Synonyms"]}]')
>>> list(map(list, res))
[['autobus', 'bus', 'charabanc', 'coach', 'double-decker', 'jitney', 'motorcoach', 'omnibus', 'passenger vehicle']]