在numpy的object_阵列最长的字符串 [英] Longest string in numpy object_ array

查看:129
本文介绍了在numpy的object_阵列最长的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用numpy的object_阵列存储可变长度的字符串,例如

I'm using a numpy object_ array to store variable length strings, e.g.

a = np.array(['hello','world','!'],dtype=np.object_)

有一种简单的方法来找到数组中最长的字符串的长度不遍历所有元素?

Is there an easy way to find the length of the longest string in the array without looping over all elements?

推荐答案

最大值(A,键= LEN)为您提供了最长的字符串(和 LEN(最大值(A,键= LEN))为您提供了它的长度),而不需要您code外在的循环,当然,最大将尽自己的内部循环,因为它不可能以任何其他方式识别中最长的字符串。

max(a, key=len) gives you the longest string (and len(max(a, key=len)) gives you its length) without requiring you to code an explicit loop, but of course max will do its own looping internally, as it couldn't possibly identify "the longest string" in any other way.

这篇关于在numpy的object_阵列最长的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆