openrefine - 如何在Openrefine中获取数组中最长的字符串
问题描述
使用 GREL 是否可以获得数组中最长的字符串?例如,如果我有一个包含 3 个字符串 ["a","aaa","aa"] 的数组,我想获得 "aaa"。
解决方案
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(',')
推荐阅读
- xml - 用于在 XML 文件中查找和替换与 CSV 文件中的值匹配的值作为参数的 Powershell 脚本
- c++ - 如何使用 std::optional 延迟初始化地图,并使用 emplace 添加到地图
- python - 这个特定的双循环在 python 中是如何工作的?解释双 for 循环
- python - 有没有办法在 csv 文件中收集多次运行的 pytest 结果?
- makefile - 为什么我的内核模块 Makefile 使用内核 4.14 而不是 5.6 构建 .ko?
- java - 从黑名单和白名单正则表达式创建正则表达式以识别和删除 url 参数
- flutter - 如何使用 TextEditingController 创建带有清除按钮的 TextField?
- php - 两次声明相同的路由,但根据中间件期望不同的行为
- python - 使用更新方法将数据附加到 json 对象只需使用 python 添加最后一个循环项
- php - 使用 php 数组和 php foreach 创建选择选项