Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Find the Longest String in a NumPy Array?

#1
How to Find the Longest String in a NumPy Array?

5/5 – (1 vote)

Python Longest String in NumPy Array


To find the longest string in a given NumPy array, say arr, you can use the max(arr, key=len) function that determines the maximum by comparing the length of the array elements using the len() function as a key for comparison.

import numpy as np arr = np.array(['Alice', 'Bob', 'Carl'])
print(max(arr, key=len))
# Alice

You can find more about the powerful max() function in our detailed blog tutorial:

? Recommended Tutorial: Python Maximum Function

Python Length of Longest String in NumPy Array


To find the length of the longest string in a NumPy array arr, use the max(arr, key=len) function to obtain the string with the maximum length and then pass this max string into the len() function to obtain the number of characters of the max string.

len(max(arr, key=len))

Here’s a more detailed code example of a simple 1D NumPy Array:

import numpy as np arr = np.array(['Alice', 'Bob', 'Carl']) # Print Longest String:
print(max(arr, key=len))
# Alice # Print Length of Longest String
print(len(max(arr, key=len)))
# 5

Get Longest String from NumPy Axis (2D, Column or Row)


To get the longest string from a certain NumPy array axis (e.g., row or column), first use simple NumPy slicing and indexing to get that axis (e.g., arr[0, :] to get the first row) and pass it into the max() function with the key argument set to the length function like so: max(arr[0, :], key=len).

Here’s an example to get the longest string of the first row of a 2D array:

import numpy as np arr = np.array([['Alice', 'Bob', 'Carl'], ['Ann', 'Zoe', 'Leonard']]) print(max(arr[0, :], key=len))
# Alice

Here’s an example to get the longest string of the third column of a 2D array:

print(max(arr[:, 2], key=len))
# Leonard

You get the idea. ?

If you want to get the longest string from the whole NumPy array, not only from a column or row or axis, first flatten it and then pass the flattened array into the max() function using the key=len argument.

? Recommended Tutorial: How to Flatten a NumPy Array?



https://www.sickgaming.net/blog/2022/09/...mpy-array/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Library Hijacking – A Simple Demonstration on NumPy xSicKxBot 0 1,249 11-22-2022, 01:48 AM
Last Post: xSicKxBot
  [Tut] Solidity Bytes and String Arrays, Concat, Allocating Memory, and Array Literals xSicKxBot 0 1,199 10-25-2022, 09:13 AM
Last Post: xSicKxBot
  [Tut] How to Convert Pandas DataFrame/Series to NumPy Array? xSicKxBot 0 1,213 10-24-2022, 02:13 PM
Last Post: xSicKxBot
  [Tut] How to Print a NumPy Array Without Scientific Notation in Python xSicKxBot 0 1,217 10-20-2022, 11:44 AM
Last Post: xSicKxBot
  [Tut] Python – Return NumPy Array From Function xSicKxBot 0 1,184 10-16-2022, 03:49 AM
Last Post: xSicKxBot
  [Tut] Python Find Longest List in List xSicKxBot 0 1,298 09-23-2022, 02:19 PM
Last Post: xSicKxBot
  [Tut] Combine Images Using Numpy xSicKxBot 0 1,217 09-18-2022, 10:23 AM
Last Post: xSicKxBot
  [Tut] How to Find All Palindromes in a Python String? xSicKxBot 0 1,280 09-16-2022, 12:52 PM
Last Post: xSicKxBot
  [Tut] How to Find a Partial String in a Python List? xSicKxBot 0 1,113 09-12-2022, 03:53 PM
Last Post: xSicKxBot
  [Tut] How to Iterate over a NumPy Array xSicKxBot 0 1,157 08-19-2022, 06:55 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016