1

I'm relatively new to computer science, and I'm learning how to code in python. I'm trying to figure out how to make a function that takes a list and returns a list of suffixes from the inputted list, in order from shortest length to longest length. For example, entering [3,4,2,-9,7,6,1] to the function would return [[],[1],[6,1],[7,6,1],[-9,7,6,1],[2,-9,7,6,1],[4,2,-9,7,6,1],[3,4,2,-9,7,6,1]]

I've tried several approaches, but so far I'm not having much luck. Here is what I have so far:

def h(m):
    newlist = []
    x = 0
    y = (len[m])-1
    while x in range(y):
        sublist = []
        sublist = sublist + m[x:y]
        newlist.append(sublist)
        x += 1
    return new list

When I try to run the function by entering something like

a = [3,4,2,-9,7,6,1]
h(a)

I get an error:

Traceback (most recent call last):
File "<pyshell#239>", line 1, in <module>
h(a)
File "<pyshell#238>", line 4, in h
y = (len[m])-1
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

My objective with this bit of code was simply to create a the list of suffixes without sorting them by length. After I figure out how to create this new list I will add the sorting bit of the code. Keep in mind this is not a homework assignment. Any help would be greatly appreciated!

3 Answers 3

3

Possible solution is using list comprehension:

[l[i:] for i in range(len(l), -1, -1)]

This code uses slicing and list comprehension to simply return a list of slices from the end. Using your code, small modification is required - len is a function and not a dictionary, therefore you need to use the call operator () instead of subscript operator [].

y = len(m) - 1

That will not yield correct result though, because you will not get the last suffix and empty suffix. In order to cover these two, you will need to modify y or the loop to cover them

y = len(m) + 1

or better

while x in range(y + 1):
Sign up to request clarification or add additional context in comments.

2 Comments

It is because range isn't inclusive from the right: range(5) actually returns [0, 1, 2, 3, 4]. So, you wouldn't do a [7:7] but [6:7]. It would be nicer to handle this case in the loop itself - making it range(y + 1).
sorry I just deleted my comment because I figured out what it's doing. Thank you so much!
0

use parenthesis to get the length of the list:

y = len(m) - 1

1 Comment

You probably should use mpolednik's answer anyway. It's more pythonic.
0

Your error is in your len:

(len[m]) - 1

len is a function, and you can't index or slice or the what not. Do this instead:

y = len(m) - 1

There's also one other error:

return new list

Should be: (You can't have spaces in variables)

return newlist

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.