user2169610
user2169610

Reputation: 3

Compare elements of list within a list

I have a list like this:

[[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]

The first element of the little lists is the "idnumber" and the other 2 elements are data I want to compare.

The thing I want to do is this: I want to delete the elements of the big list for which at least one of the two data numbers is under 250. The answer should look like this:

[[849401, 971, 572], [848613, 790, 596], [846978, 914, 272]].

I tried to make a for loop in a for loop but I failed:

def zonderRuis(sigIdSpot=[[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]):
    ruisvrij=[]
    for i in range(len(sigIdSpot)):
        for r in i:
            if r[2]>=250 and r[3]>=250:
                ruisvrij.append(r)
    return ruisvrij

Upvotes: 0

Views: 120

Answers (4)

ekinoshita
ekinoshita

Reputation: 55

Just a point about iteration:

for i in range(len(sigIdSpot)):
    x = sigIdSpot[i]
    print x

# becomes more clear if you do the following:
for x in sigIdSpot:
    print x

Now if you want to debug your code, you can put some prints to see what's going on with your code:

def zonderRuis(sigIdSpot=[[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]):
    ruisvrij=[]
    for i in range(len(sigIdSpot)):
        print 'i is:', i
        for r in i:
            print 'r is:', r
            if r[2]>=250 and r[3]>=250:
                ruisvrij.append(r)
                print 'r appended to ruisvrij:', ruisvrij
    return ruisvrij

Remember, the interactive shell is our best friend.

Upvotes: 1

het.oosten
het.oosten

Reputation: 885

list1 =  [[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]
list2 = []
for x in list1:
    if x[1] > 250 and x[2] > 250:
        lijst1.append(x)

Upvotes: 0

NPE
NPE

Reputation: 500167

In [15]: l = [[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]

In [16]: [sub for sub in l if min(sub[1:]) >= 250]
Out[16]: [[849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]

Upvotes: 0

YXD
YXD

Reputation: 32511

[x for x in data if all(y >= 250 for y in x[1:])]

Upvotes: 8

Related Questions