Ask Your Question
0

How to find all maximum length lists with a nested and display?

asked 2020-07-04 17:45:36 +0100

sriram gravatar image

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

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
0

answered 2020-07-04 18:45:10 +0100

dsejas gravatar image

Hello, 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!

edit flag offensive delete link more
0

answered 2020-07-04 19:02:31 +0100

curios_mind gravatar image

As 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))
edit flag offensive delete link more
0

answered 2020-07-04 18:56:35 +0100

philipp7 gravatar image

updated 2020-07-04 18:57:52 +0100

You 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-07-04 17:45:36 +0100

Seen: 1,248 times

Last updated: Jul 04 '20