remotesatellite
remotesatellite

Reputation: 61

Apply if statement on multiple lists with multiple conditions

I would like to append ids to a list which meet a specific condition.

    output = []
    areac = [4, 4, 4, 4, 1, 6, 7,8,9,6, 10, 11]
    arean = [1, 1, 1, 4, 5, 6, 7,8,9,10, 10, 10]
    id = [1, 2, 3, 4, 5, 6, 7,8,9,10, 11, 12]
    dist = [2, 2, 2, 4, 5, 6, 7.2,5,5,5, 8.5, 9.1]
    for a,b,c,d in zip(areac,arean,id,dist):
            if a >= 5 and b==b and d >= 3:
                output.append(c)
                print(comp)
            else:
                pass
The condition is the following:
- areacount has to be >= 5
- At least 3 ids with a distance of >= 3 with the same area_number

So the id output should be [10,11,12].I already tried a different attempt with Counter that didn't work out. Thanks for your help!

Upvotes: 1

Views: 602

Answers (1)

re-za
re-za

Reputation: 790

Here you go:

I changed the list names to something more descriptive.

output = []
area_counts = [4, 4, 4, 4, 1, 6, 7, 8, 9, 6, 10, 11]
area_numbers = [1, 1, 1, 4, 5, 6, 7, 8, 9, 10, 10, 10]
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
distances = [2, 2, 2, 4, 5, 6, 7.2, 5, 5, 5, 8.5, 9.1]

temp_numbers, temp_ids = [], []
for count, number, id, distance in zip(counts, numbers, ids, distances):
    if count >= 5 and distance >= 3:
        temp_numbers.append(number)
        temp_ids.append(id)

for (number, id) in zip(temp_numbers, temp_ids):
    if temp_numbers.count(number) == 3:
        output.append(id)

output will be:

[10, 11, 12]

Upvotes: 1

Related Questions