首页 > 解决方案 > 如何在Openrefine中获取数组中最长的字符串

问题描述

使用 GREL 是否可以获得数组中最长的字符串?例如,如果我有一个包含 3 个字符串 ["a","aaa","aa"] 的数组,我想获得 "aaa"。

标签: openrefine

解决方案


You can probably do that at the cost of a very complicated formula. It's typically to face this kind of case that Open Refine added Python (and Clojure) as scripting languages. Even if you don't know Python, you can find in two minutes the answer to the question "how to choose the longest string in list?" and simply copy and paste it (by adding a "return" instead of "print")

In this case :

return max(['a','aaa','aaaa','aa'], key=len)

EDIT

Just for the sake of the challenge, here is a possible solution with GREL.

value = "a,aa,aaaa,aa"

forEach(value.split(','), e, if(length(e)==sort(forEach(value.split(','), e, e.length()))[-1], e, null)).join(',').split(',')

enter image description here


推荐阅读