在numpy的object

编程入门 行业动态 更新时间:2024-10-10 08:23:02
本文介绍了在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

本文发布于:2023-11-29 10:48:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:numpy   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!