1

Hey I'm trying to write a function that only prints a list that contains no zeros in a list of lists. Here is a sample of the list of lists I am working with:

[['10011.0', ' 65301.0', ' 0.085', ' 0.0', ' 0.0', ' 0.0', ' 0.03', ' 0.03', ' 0.075',
 ' 0.05', ' 0.065', ' 0.05', ' 0.05', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0'],
 ['10017.0', ' 2743.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.2', ' 0.413333', ' 0.415', ' 0.3125', ' 0.45', ' 0.46', ' 0.55'],
 ['10017.0', ' 9262.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.35', ' 0.69', ' 0.675', ' 0.8075', ' 0.8075', ' 0.5325', ' 0.785'], 
 ['10017.0', ' 29319.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0225', ' 0.06', ' 0.0575', ' 0.105', ' 0.1', ' 0.045', ' 0.0'],
 ['10017.0', ' 43562.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.0', ' 0.106667', ' 0.0925', ' 0.09', ' 0.1', ' 0.09', ' 0.1025'],
 ['10017.0', ' 43563.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 0.106667', ' 0.0925', ' 0.09', ' 0.1', ' 0.09', ' 0.1028']]

The code I am running is:

def no_zero(A):
for i in range(len(A)):
    for j in range(2, (len(A[i])+1)):
        if '0.0' not in A[i]:
            print A[i]
            break

For some reason it does not filter any of the lists and prints the entire list of lists despite the conditional: "if '0.0' not in A[i]:" I am unsure where my error is because it seems like the logic should be fairly straightforward. Thanks for any help!

3
  • 3
    Why do you have the second for loop? You never use j anywhere. Commented Jul 20, 2014 at 3:06
  • I need it to skip the first two elements of the list. Sorry for not clarifying that in the initial question. Commented Jul 20, 2014 at 3:10
  • Alright, but you still never use j anywhere in your code. You also don't want to use len+1 as your termination point, because A[len(A)] never exists. Commented Jul 20, 2014 at 3:12

4 Answers 4

3

Normally when you want to filter a list of items, we use list comprehension with the filtering condition to get a new list. So, in your case, it can be done like this

[c_list for c_list in list_of_lists if '0.0' not in c_list]

This will give all the lists with no 0.0 in them. If you want to skip the first two elements, then start from the third element, while checking

[c_list for c_list in list_of_lists if '0.0' not in c_list[2:]]
Sign up to request clarification or add additional context in comments.

2 Comments

It should be [2:], not [3:]. Lists are zero-indexed.
Except the zeros in his lists are ' 0.0' and I'd not count on that particular spacing. `Tis better to use floats; at least 0.0 is guaranteed precise representation in IEEE-754.
1

You are making this much harder than it needs to be.

  1. List indices are often unnecessary in Python; use iteration instead.
  2. Testing strings for equality to '0.0' is troublesome when your data has spaces ' 0.0'
  3. List comprehensions and built-ins are your friends.

Thus:

 def no_zeros(rows):
    """Takes a list of rows, prints each row if and only if it has no zeros."""
    for row in rows:
        # skip first two elements of row, per specification
        tests = [float(x) != 0 for x in row[2:]]
        if all(tests):
            print row

yields:

>>> no_zeros(A)
['10017.0', ' 43563.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 1.0', 
 ' 1.0', ' 1.0', ' 1.0', ' 1.0', ' 0.106667', ' 0.0925', ' 0.09', ' 0.1', 
 ' 0.09', ' 0.1028']

Comments

1

Noobie attempt. This is if you want to test if the value is 0.

Whereby l is your list in list.

for i in l:
    a=[]
    for j in i[2:]:
        if float(j)==0:
          break
    else:
        a.append(i)

2 Comments

+1 pretty good for a noob. You even got the for / break / else thing right. You missed the for j in i[2:]: thing but that was hidden in a comment, not in the OP question.
Nah, I missed that "skip the first two elements" myself at first. Be kind to yourself ;)
0

If A is your list of lists, filter(all, A)

1 Comment

Did you test it? A a-s an iterable is a list of length 6 (each of which is a list itself. A non-empty list is "trueish" so each of those lists in A will be true for all thus a list of all 6 lists will be returned.

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.