-1

I have a million rows and I need to find the max character length of a field. For example, the data might look like this:

[
  ['john doe', 12, 2.99],
  ['jane doe', 29, 3.99],
]

And to figure out the maxLength of the first column I would do:

maxLength = 0
for row in rows:
    maxLength = max(len(row[0]), maxLength)

However, determining the len(str) seems like an expensive operation. Is there a better way to do this, even perhaps approximating the max length, etc? An answer in python or C/C++ is fine -- I'm just looking for how it would be done.

David542
  • 104,438
  • 178
  • 489
  • 842
  • I know little Python, but in modern C++ getting the length of a `std::string` is [guaranteed to be dirt cheap](https://en.cppreference.com/w/cpp/string/basic_string/size). A naive iteration through the list would be reasonable. – user4581301 Aug 19 '19 at 19:41
  • 1
    `len(str)` is [O(1)](https://stackoverflow.com/questions/1115313/cost-of-len-function). If you still don't want to use it, you can perform one pass through your rows and preprocess the length of each one, then your function wouldnt need to call `len(row[0])`, but some preprocessed value instead. – Daniel Aug 19 '19 at 19:45

1 Answers1

0

Use Python's built-in max with a key function using len, which is O(1) for strings as mentioned in answers to this question:

max_row = max(a, key = lambda r : len(r[0]))
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
  • It's max character length though, not the max value. But basically the question is more about if there's a better way to find the maximum string length in a row. – David542 Aug 19 '19 at 19:37
  • @David542 Yeah I just added `len`, which is `O(1)` by the way. – DjaouadNM Aug 19 '19 at 19:44