user124123
user124123

Reputation: 1683

check two values in list element python

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.

Upvotes: 0

Views: 172

Answers (4)

unutbu
unutbu

Reputation: 879341

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)

Upvotes: 0

Sukrit Kalra
Sukrit Kalra

Reputation: 34493

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.

Upvotes: 2

IcyFlame
IcyFlame

Reputation: 5199

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]

Upvotes: 0

mgilson
mgilson

Reputation: 309881

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.

Upvotes: 3

Related Questions