Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Suppose I had a list where each list element was made up of three parts like:

[[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0], [0, 0, 4], [0, 1, 3], [0, 2, 2], [0, 3, 1], [0, 4, 0], [1, 0, 3], [1, 1, 2], [1, 2, 1], [1, 3, 0], [2, 0, 2], [2, 1, 1], [2, 2, 0], [3, 0, 1], [3, 1, 0], [4, 0, 0]]

would there be a way to check what is inside each list element i.e say I wanted to create a new list, based on the above containing the index position of all elements that contain two zeroes, and also a list of every element containing one zero, how would I do this?

I am aware as to how to check if a single thing is in a list element, but not if that element is occurs twice.

share|improve this question

4 Answers

up vote 3 down vote accepted

You could use the .count method.

two_zeros = [x for x in lst if x.count(0) == 2]
one_zero = [x for x in lst if x.count(0) == 1]

If you wanted to be really clever, you could do the whole thing in a single loop with a collections.defaultdict:

d = collections.defaultdict(list)
for sublist in lst:
    d[sublist.count(0)].append(sublist)

Now you have a mapping of number of zeros to sublists which contain that number of zeros.

Of course, if you actually want a list of the indices, you could use enumerate.

share|improve this answer
I like that for loop, will let you mnow how it goes... – user1987097 Jun 18 at 15:47

Use count().

From help:

count(...)
    L.count(value) -> integer -- return number of occurrences of value

Example Code (Part of your list) -

>>> startList = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1]]
>>> for element in startList:
        element.count(0)
2
2
2
2
1
2
1
1
2
2
1
1

How to create your lists? Use the above idea with list comprehension.

>>> twoZero = [index for index, elem in enumerate(startList) if elem.count(0) == 2]
>>> twoZero
[0, 1, 2, 3, 5, 8, 9]
>>> oneZero = [index for index, elem in enumerate(startList) if elem.count(0) == 1]
>>> oneZero
[4, 6, 7, 10, 11]

This is for a part of your list.

share|improve this answer

The list reqd will have all the indices that you need:

>>> reqd = []
>>> for i in range(len(d)):

    if d[i].count(0) == 2:
        reqd.append(i)


>>> reqd
[0, 1, 2, 3, 5, 8, 9, 12, 18, 19, 23, 33]
share|improve this answer

You could use a list comprehension, list.count and enumerate:

two_zeros_index = [i for i, item in enumerate(datalist) 
                   if item.count(0) == 2]

But since you want to create two lists, a plain-old for-loop might be better (so you only iterate through datalist once):

one_zero, two_zeros_index = [], []
for i, item in enumerate(datalist): 
    n = item.count(0)
    if n == 1:
        # Every *item* containing one zero
        one_zero.append(item)
    elif n == 2: 
        # Every *index* containing two zeros
        two_zeros_index.append(i)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.