0

I apologize if there is incorrect terminology through this post, I am relatively new to python and am trying something out of my wheel house.

I am trying to build a program that will add a value to the values within a nested list.

see nested list below.

List = [ nan, 4, 5, nan, nan, nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 5, nan, nan, nan, nan]

I would like the program to start at the second list and end before the final list in the sequence and also to skip the first and the last values within lists two through five. Essentially only adding values to the places where the 2 and the 3 are.

The solution should look like this:

List = [ nan, 4, 5, nan, nan, nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 5, nan, nan, nan, nan]

I tried to tackle this as follows: Firstly, to create a 'for' loop range boundary condition that the program would iterate within, Since lists start at 0 and end at -1 I could add a range of (1,-2) and that would then exclude the first and last of the nested lists from the List. Next a nested 'for' loop that would, using enumerate, iterate the values within the individual nested list range I am interested in, the 2 and the 3 spot.

for index in range (1,-1):
   for index, item in enumerate(List, start = 1):     
      if item > 1:
         List[index] = round((List[index]+1),3)
print(List)

Enumerate function has a start function that if I set the 'if' statement to greater than 1 it would skip the nan values and start at the 2. I am unsure how to get the program to stop before the 5. Right now the program runs but none of the numbers change.

Any help would be appreciated.

Thank you.

EDIT: I have updated my example_list and code below:

example_list = [[ 0, 4, 5, 0, 0, 0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 5, 0, 0, 0, 0]]

x = 1

for i, sub_list in enumerate(example_list[1:-1],start=1):
    for j, entry in enumerate(sub_list[1:-1][1:-1], start=2):
           if j > 1:
               example_list[j] = round((example_list[j]+    x),3)
           example_list[i][j] += 1
print(example_list)

I am running into a TypeError: can only concatenate list (not "int") to list error.

2 Answers 2

0

Your question is not completely well posed as already pointed out, but here are some ideas on how to solve your problem:

If you have a nested list as

example_list = [[ nan, 4, 5, nan, nan, nan, nan], 
   [ nan, 4, 2,  3,   5,  nan, nan], 
   [ nan, 4, 2,  3,   5,  nan, nan], 
   [ nan, 4, 2,  3,   5,  nan, nan], 
   [ nan, 4, 2,  3,   5,  nan, nan], 
   [ nan, 4, 5, nan, nan, nan, nan]
]

then you can use the following snippet to

start at the second list and end before the final list in the sequence and also to skip the first and the last values within lists two through five

for i, sub_list in enumerate(example_list[1:-1],start=1):
    for j, entry in enumerate(sub_list[1:-1],start=1):
        # Perform an operation with that value, e.g. add 1
        example_list[i][j] += 1

Essentially only adding values to the places where the 2 and the 3 are.

Regarding the 2 and 3's in your list, they are only in columns 2 and 3, so you could use the following code snippet:

for i, sub_list in enumerate(example_list[1:-1], start=1):
    for j, entry in enumerate(sub_list[2:4], start=2):
        # Perform an operation with that value, e.g. add 1
        example_list[i][j] += 1

I hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

my apologies for the poor explanation of my problem. I am attempting to add my section of code perform an operation under the [j] for loop. the addition of this section of code Additional_Value = 1 if j > 1: example_list[j] = round((example_list[j]+ Additional_Value),3). I am getting a TypeError: can only concatenate list (not "int") to list. that I have never seen before. Is this something you have run into in the past and would feel comfortable helping me navigate? Thank you for your time.
It is really hard to read code in the comments. Maybe you can edit your original question and post the exact example script that you have problems with such that it is easier to read and such that others can try to run your code snippet on their local machines. In case you have a nested list as in my code snippet, then the problem is that List[index] is itself a list. You need to access it with List[index1][index2] to get entry at index2 in the sub-list index1.
0

There are several problems with both your approach and your current code:

  1. The syntax of how you define your inputs is invalid and will throw an IndentationError. If you want a nested list, you have to nest square brackets, e.g.:
inputs = [
    [1, 2, 3],
    [4, 5, 6]
]
  1. nan does not exist in Python, do you mean numpy.nan?

  2. range(1, -1) probably does no do what you think. Try printing out intermediate values like indices to get a feeling for what your code does. Also, try starting with the smallest possible input data to make it easier to follow.

  3. Give descriptive names to your variables to reduce confusion. In particular, do not use their types as their names (like List). If it's really an abstract list of numbers, you could call it numbers, although something more meaningful would be preferable.

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.