3

Sorry if this has already been answered, I'm very new to python, I've had a good look around and have found this page here which has helped me a little but i'm still stuck.

I'm trying to get any type of input to work in my script, I've got it working for a single item and a list of items, but i'm finding it hard to get it to work for a list of lists.

I've edited the code as per comments to make a little more sense:

Input = [[1,2,3],[4,5,6],[7,8,9]]

if isinstance(Input, list):
    Input = Input
else:
    Input = [Input]

listout = []


for x in Input:
    listout.append(x+2)

print (listout)

returns: line 12, in listout.append(x+2) TypeError: can only concatenate list (not "int") to list

This works if Input = 1 or Input = [1,2,3,4] for example, but not for the above.

I would like the output to look like the below for a list of lists:

[[3,4,5],[6,7,8],[9,10,11]]

I attempted to make a flat list out of the nested lists first but i'd like to keep the list structure for the output.

Thanks all for reading,

TJ

4
  • Flatten a list with a list comprehension Commented Aug 31, 2017 at 13:59
  • maybe you could use pre-defined Inputs instead of using some library for that. Maybe just Input = 1, Input = [1,2,3], etc. and then explain based on these what doesn't work (or what you want). Commented Aug 31, 2017 at 14:00
  • 1
    I recommend to avoid writing code that attempts to handle inhomogeneous input in the first place. It results in ugly code. Commented Aug 31, 2017 at 14:50
  • @wim so you recommend having two separate scripts? One for nested lists and one for a single input/list? Commented Aug 31, 2017 at 14:57

1 Answer 1

2

You might consider numpy:

>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> a+=2
>>> a
array([[ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> a.tolist()
[[3, 4, 5], [6, 7, 8], [9, 10, 11]]

If you cannot use numpy, you will need to write a recursive procedure for arbitrary nesting:

def nested_sum(e,n):
    if isinstance(e, list):
        return [nested_sum(x, n) for x in e]
    else:
        return e+n    

>>> nested_sum([1,[2,3],[4,5,6],[7,[8],9]], 2)
[3, [4, 5], [6, 7, 8], [9, [10], 11]]

If you just have two levels of nesting (as you have in the example), you can do a list comprehension:

>>> li=[[1,2,3],[4,5,6],[7,8,9]]
>>> [[e+2 for e in sl] for sl in li]
[[3, 4, 5], [6, 7, 8], [9, 10, 11]]
Sign up to request clarification or add additional context in comments.

3 Comments

thanks this does work, but I'm using this in a programme which uses a .NET based ironpython implementation so it doesn't support numpy (to the best of my knowledge).
Updated. Try the recursive or list comprehension.
Cheers @dawg you're a star, the nested_sum definition is exactly what I was looking for.

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.