1

So I am trying to add all the columns of a 2D array except for the first two columns of the array. If the sum of the row is greater than or equal to 9 and less than 12, I want the function to print the row. Here is a sample of my 2D array that is a list of lists:

[[12606.000,  74204.000,     1.000,     1.000,     1.000,     1.000,     1.000,     0.000,     0.000],        
[12606.000,  105492.000,     0.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000],    
[12606.000,  112151.000,     1.000,     1.000,     0.000,     0.000,     0.000,     0.000,     0.000],     
[12606.000,  121896.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000,     0.000]]     

(Some columns were deleted for formatting). Here is my code:

def sumBinRow(A):
    """Returns the list of employees recording data for at least nine months and fewer than twelve.
    """
    for i in range(len(A)):
        for j in range(len(A[i])):
            if 9 <= sum(A[i][j+2]) <12:
                print A[i]

I keep getting a "Type Error" saying the 'int' object is not iterable.

8
  • Sort of. It's a project for my internship Commented Jun 23, 2014 at 18:02
  • I get the same error when I try: "for j in range(12)" as well Commented Jun 23, 2014 at 18:02
  • Sorry... Wrong thread Commented Jun 23, 2014 at 18:11
  • So you call your function with the argument being the list of lists above? Commented Jun 23, 2014 at 18:15
  • Yes A is the list of lists that is the input of the function Commented Jun 23, 2014 at 18:15

4 Answers 4

1

Basically, you need to iterate over each list, for each list, take a slice of that list starting from 2, then sum that and do your comparison.

def sumBinRow(A):
    """Prints the list of employees recording data for at least nine months and fewer than twelve.
    """
    for row in A:
        if 9 <= sum(row[2:]) < 12:
            print row

or in 1 line cause why not :P

def sumBinRow(A):
    """Prints the list of employees recording data for at least nine months and fewer than twelve.
    """
    print [row for row in A if 9 <= sum(row[2:]) < 12]
Sign up to request clarification or add additional context in comments.

1 Comment

Was going to suggest just the same thing. Although I'd adapt the name and the docstring to what the function actually does...
0

You should should sum like this.

def sumBinRow(A):
    """Returns the list of employees recording data for at least nine months and fewer than twelve.
    """
    for i in range(len(A)):
        sigma = 0
        for j in range(2,len(A[i])):
            sigma += A[i][j]
            if 9 <= sigma <12:
                print A[i]

2 Comments

I understand your solution and really like it but for some reason the function is returning rows that have all 1's
[12227.0, 38606.0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [12227.0, 38606.0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [12227.0, 67307.0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [12227.0, 67307.0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
0

If you use NumPy for this task it would be simpler and much faster:

import numpy as np

a = np.array([[12606.000,  74204.000,     1.000,     1.000,     1.000,     1.000,     1.000,     0.000,     0.000],
              [12606.000,  105492.000,     0.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000],
              [12606.000,  112151.000,     1.000,     1.000,     0.000,     0.000,     0.000,     0.000,     0.000],
              [12606.000,  121896.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000,     0.000]])

b = a[:, 2:].sum(axis=1)
check = (b >= 9) & (b < 12)
print(a[check])

Comments

0

The problem is that you are trying to sum over something that is not an array:

if 9 <= sum(A[i][j+2]) <12: // A[j][j+2] is not iterable, it is an int

Instead you want to use the slice function:

if 9 <= sum(A[i][2:]) <12:  // This will sum sum all but first two elements

A more pythonic way would be to use a list comprehension. This can be done with a single line of code:

A = [[12606, 74204 , 1, 1, 1, 1, 1, 0, 0],        
     [12606, 105492, 0, 0, 0, 0, 0, 0, 1],    
     [12606, 112151, 1, 1, 0, 0, 0, 0, 0],     
     [12606, 121896, 0, 0, 0, 0, 0, 1, 0]]

print [row for row in A if 9 <= sum(row[2:]) < 12]

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.