How to use an anonymous function with juliacall in python

I am new to julia and juliacall and i would like to use the following code in julia in python exploiting juliacall:

sort(collect(countmap(x)), by = tuple -> last(tuple), rev=true)

In my code i didn’t know how to convert the anonymous function (tuple -> last(tuple)). x is the type Vector{DitStr{2, 8, Int64}}. My attempt was:

from juliacall import Main as jl

jl.sort(jl.collect(jl.countmap(x)), by= jl.last(tuple), rev=true)

but i got this error:

Traceback (most recent call last):
  File "/home/path/PycharmProjects/path/", line 156, in <module>
    start = time()
  File "/home/path/PycharmProjects/path/", line 84, in check_IS
    return convert_DitStr_to_python(x, b)
  File "/home/path/PycharmProjects/path/", line 102, in convert_DitStr_to_python
    top = jl.sort(jl.collect(jl.countmap(x)), by= jl.last(tuple), rev=true)
  File "/home/path/.julia/packages/PythonCall/1f5yE/src/jlwrap/any.jl", line 208, in __call__
    return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
AttributeError: type object 'tuple' has no attribute 'n'

This is the framework, but i would like to know how to use an anonymous function with juliacall

Did you try the standard Python lambda?

1 Like

Yes, I did. But unfortunately it gave me back an error about “True” because it didn’t match “true”.

What if you are using juliacall.convert as the last call of the lambda?

1 Like

I didn’t try with juliacall.convert because i am not really familiar with it. How could i use it?

I have found this workaround:

jl.sort(jl.collect(, jl.keys(jl.countmap(x)))), rev=True)

I noticed that the previous error was on “True” was my typo, so maybe it can work the standard lambda function. Thanks for the help

1 Like
jl.convert(jl.Bool, something that generates True)

However, after looking at the conversion rules, I don’t think this explicit call should be needed.

Anyway - I noticed your comment above regarding the typo - and your workaround. Happy that it works.