Finding length of the longest list in an irregular list of lists
Here is a recursive solution for any depth list:
def longest(l):
if not isinstance(l, list):
return 0
return max(
[len(l)]
+ [len(subl) for subl in l if isinstance(subl, list)]
+ [longest(subl) for subl in l]
)
How to find the longest list in a list?
This will return the longest list in the list values
:
max(values, key=len)
Maximum length list from list of lists using max with key
I believe it should work because according to my (apparently false) understanding the above statement would be equivalent to applying length to each element and then invoking
max
:
No, The code
maximum = max(lists, key=len)
Means, find an "element" from lists
which having maximum value of len(element)
Whereas, what you intended to find value of len(element)
. one line quvilent of your code:
maximum = 0
for l in lists:
maximum = max(maximum, len(l))
would be
max(map(len, lists))
or may be using len(max(lists, key=len))
Find max length of each column in a list of lists
[max(len(str(x)) for x in line) for line in zip(*foo)]
Python's most efficient way to choose longest string in list?
From the Python documentation itself, you can use max
:
>>> mylist = ['123','123456','1234']
>>> print max(mylist, key=len)
123456
irregular list of lists to dataframe
Here's a lapply
/ mapply
example...
# Data
set.seed(1)
ll <- replicate( 4 , runif( sample(4,1) ) )
str(ll)
#List of 4
# $ : num [1:2] 0.372 0.573
# $ : num [1:4] 0.202 0.898 0.945 0.661
# $ : num [1:3] 0.0618 0.206 0.1766
# $ : num [1:3] 0.384 0.77 0.498
# Find length of each list element
len <- sapply(ll,length)
# Longest gives number of rows
n <- max( len )
# Number of NAs to fill for column shorter than longest
len <- n - len
# Output
mapply( function(x,y) c( x , rep( NA , y ) ) , ll , len )
# [,1] [,2] [,3] [,4]
#[1,] 0.3721239 0.2016819 0.06178627 0.3841037
#[2,] 0.5728534 0.8983897 0.20597457 0.7698414
#[3,] NA 0.9446753 0.17655675 0.4976992
#[4,] NA 0.6607978 NA NA
Note, output is a matrix, so you need to wrap the output with data.frame()
.
Row-wise filling and returning a data.frame
data.frame( t( mapply( function(x,y) c( x , rep( NA , y ) ) , ll , len ) ) )
# X1 X2 X3 X4
#1 0.37212390 0.5728534 NA NA
#2 0.20168193 0.8983897 0.9446753 0.6607978
#3 0.06178627 0.2059746 0.1765568 NA
#4 0.38410372 0.7698414 0.4976992 NA
Convert list of lists with different lengths to a numpy array
you could make a numpy array with np.zeros and fill them with your list elements as shown below.
a = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
import numpy as np
b = np.zeros([len(a),len(max(a,key = lambda x: len(x)))])
for i,j in enumerate(a):
b[i][0:len(j)] = j
results in
[[ 1. 2. 3. 0.]
[ 4. 5. 0. 0.]
[ 6. 7. 8. 9.]]
Length of string in sublist within a list python
Using max
with a key function to get the list with longest strings:
>>> L = [['spear', 'pears', 'reaps'], ['monster', 'sternom']]
>>> max(L, key=lambda xs: len(xs[0]))
['monster', 'sternom']
UPDATE
What if there were multiple sublists with longest of the same length?
Find the maximum length. Filter sublist based on the length:
>>> L = [['largest', 'artlegs'], ['spear', 'pears', 'reaps'], ['monster', 'sternom']]
>>> M = max(len(xs[0]) for xs in L)
>>> [xs for xs in L if len(xs[0]) == M]
[['largest', 'artlegs'], ['monster', 'sternom']]
Is there a zip-like function that pads to longest length?
In Python 3 you can use itertools.zip_longest
>>> list(itertools.zip_longest(a, b, c))
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
You can pad with a different value than None
by using the fillvalue
parameter:
>>> list(itertools.zip_longest(a, b, c, fillvalue='foo'))
[('a1', 'b1', 'c1'), ('foo', 'b2', 'c2'), ('foo', 'b3', 'foo')]
With Python 2 you can either use itertools.izip_longest
(Python 2.6+), or you can use map
with None
. It is a little known feature of map
(but map
changed in Python 3.x, so this only works in Python 2.x).
>>> map(None, a, b, c)
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
Related Topics
Increment Values in a List of Lists Starting from 1
Get the Mean Across Multiple Pandas Dataframes
Selecting Specific Rows of CSV Based on a Column'S Value in Python
Tensorflow:Attributeerror: 'Module' Object Has No Attribute 'Mul'
Typeerror: Strptime() Argument 1 Must Be Str, Not List
Python Pip Install Error [Ssl: Certificate_Verify_Failed]
Convert a Standard Python Key Value Dictionary List to Pyspark Data Frame
Importerror: No Module Named Bs4 (Beautifulsoup)
How to Convert a Float into Hex
Putting Multiple Conditions Using Np.Where on Python Pandas
Why Am I Getting Ioerror: [Errno 13] Permission Denied
Using Pyserial to Send Binary Data
Tf.Data.Dataset: How to Get the Dataset Size (Number of Elements in an Epoch)
Create an Array With a Pre Determined Mean and Standard Deviation
Matplotlib Rotate Image File by X Degrees
In Python, How to Check If Selenium Webdriver Has Quit or Not