ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 04 Jul 2020 19:02:31 +0200How to find all maximum length lists with a nested and display?https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/ How to find all maximum length lists with a nested and display?
L=[[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]
Now i need to find the maximum length lists in this
that the program has to display
[1,2,3,4]
[1,4,5,6]
[9,10,12,8]
and say maximum length is 4
I will input some arbitrary nested list L like above and it has to do this
As i am novice to sagemath kind help
Or can we sort the nested list based on its inner lists lengths
Sat, 04 Jul 2020 17:45:36 +0200https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/Answer by dsejas for <p>How to find all maximum length lists with a nested and display?</p>
<p>L=[[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]</p>
<p>Now i need to find the maximum length lists in this</p>
<p>that the program has to display </p>
<p>[1,2,3,4]
[1,4,5,6]
[9,10,12,8]
and say maximum length is 4 <br>
I will input some arbitrary nested list L like above and it has to do this
As i am novice to sagemath kind help</p>
<p>Or can we sort the nested list based on its inner lists lengths</p>
https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/?answer=52322#post-id-52322Hello, sriram! I did the foloowing code very rapidly, so I am not sure if it is optimal or not, but it works.
Let's define the following function:
def find_max_len(L):
max_len = 0 # this stores the maximum length
max_lis = [] # this stores the maximum length lists
for li in L:
n = len(li) # the length of the current list
if n > max_len: # if the current list has a length grater than what we thought was the maximum length...
max_len = n # update the maximum length
max_lis = [li] # discard the previous lists (which are shorter), and include the current one
elif n == max_len: # if the current list has the same length as what we think to be the maximum length...
max_lis.append(li) # add the current list to the set of maximum length lists
print('Maximum length:', max_len) # print the maximum length
for li in max_lis:
print(li, end=' ') # print the maximum length lists, separated by two spaces
print() # print a new empty line
Now, you can do the following:
L=[[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]
find_max_len(L)
The output is:
Maximum length: 4
[1, 2, 3, 4] [1, 4, 5, 6] [9, 10, 12, 8]
I assume there should be a better programatical way of doing this (perhaps with list comprehensions), but I didn't have the time to explore that possibility. If I find another way to do this, I'll post it as an update.
I hope this helps!Sat, 04 Jul 2020 18:45:10 +0200https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/?answer=52322#post-id-52322Answer by curios_mind for <p>How to find all maximum length lists with a nested and display?</p>
<p>L=[[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]</p>
<p>Now i need to find the maximum length lists in this</p>
<p>that the program has to display </p>
<p>[1,2,3,4]
[1,4,5,6]
[9,10,12,8]
and say maximum length is 4 <br>
I will input some arbitrary nested list L like above and it has to do this
As i am novice to sagemath kind help</p>
<p>Or can we sort the nested list based on its inner lists lengths</p>
https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/?answer=52324#post-id-52324As the other contributors have said, you can simply use python.
Just another version:
a=[[1,3],[4,5,6],[2,3,4,6,7],[1,3]]
givememaxlen=lambda x: max([len(i) for i in x]);
print(givememaxlen(a))Sat, 04 Jul 2020 19:02:31 +0200https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/?answer=52324#post-id-52324Answer by philipp7 for <p>How to find all maximum length lists with a nested and display?</p>
<p>L=[[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]</p>
<p>Now i need to find the maximum length lists in this</p>
<p>that the program has to display </p>
<p>[1,2,3,4]
[1,4,5,6]
[9,10,12,8]
and say maximum length is 4 <br>
I will input some arbitrary nested list L like above and it has to do this
As i am novice to sagemath kind help</p>
<p>Or can we sort the nested list based on its inner lists lengths</p>
https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/?answer=52323#post-id-52323You don't need Sage for that, you can use pure Python. Here is a solution with list comprehensions:
L = [[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]
lengths = list(len(l) for l in L)
maxLength = max( len(l) for l in L )
print(maxLength)
listWithMaxLength = list(l for l in L if len(l) == maxLength)
print(listWithMaxLength)
First we create the list `lengths` with the lengths of the individual lists. Then we can simply get the maximum length as the maximum of that list. Now we create a new list `listWithMaxLength` where we put all the lists from `L` in which have this maximum length (for this you can also use the method `filter` from python). I'm sure there are faster methods to do that but for reasonable inputs this should work absolutely fine.Sat, 04 Jul 2020 18:56:35 +0200https://ask.sagemath.org/question/52320/how-to-find-all-maximum-length-lists-with-a-nested-and-display/?answer=52323#post-id-52323